What is the value of the expression 17 / 5 in Java, where both operands are of type int?
Using Objects And Methods
Question · Easy
Given String s = "Computer";, what does s.substring(2, 5) return?
What does the expression Math.abs(-7) + Math.pow(2, 3) evaluate to?
Question · Medium
Consider: double d = (double) 9 / 4; What value is stored in d?
Given String word = "banana";, what does word.indexOf("an") return?
What is printed by the following code? String a = "cat"; String b = "car"; System.out.println(a.compareTo(b) > 0);
Which expression correctly tests whether two String references s1 and s2 contain the same sequence of characters?
A method has the signature public void change(int x). Inside change, x is reassigned to x * 2. After calling change(n) where int n = 5, what is the value of n?
Which of the following correctly declares an ArrayList that stores Integer values?
What does the following print? String s = "Hello"; System.out.println(s.length());
Given int x = 7; double y = x; what is the value of y, and is the assignment legal?
What is the result of (int) 4.99 in Java?
Consider String s = "programming";. What does s.substring(7) return?
Which statement about object references in Java is true?
What does Math.sqrt(144) return, and what is its type?
Given that an Integer object can be used where an int is expected and vice versa, what is this automatic conversion called?
What is printed? int a = 5, b = 2; System.out.println(a % b);
A method is declared as public int compute(int a, int b). Which of the following is also a valid overloaded version in the same class?
Question · Hard
What does "AP".concat("CSA") return?
Consider int total = 3 + 4 + "5"; Which statement is correct?
What is the value of the boolean expression (5 > 3) && (2 > 4)?
Selection And Iteration
How many times does the body of this loop execute? for (int i = 0; i < 5; i++) { /* body */ }
What is printed? int x = 10; if (x > 5) { System.out.print("A"); } if (x > 8) { System.out.print("B"); } else { System.out.print("C"); }
Why does short-circuit evaluation make the expression (n != 0) && (100 / n > 5) safe?
What does this loop print? int sum = 0; for (int i = 1; i <= 4; i++) { sum += i; } System.out.println(sum);
What is the negation of the condition (x > 0 && y < 10) using De Morgan's Law?
How many total iterations does the inner statement run? for (int i = 0; i < 3; i++) { for (int j = 0; j < 4; j++) { /* inner */ } }
What is printed? int i = 0; while (i < 10) { i += 3; } System.out.println(i);
Which loop correctly traverses every character of String s using index variables?
What does this print? int count = 0; for (int i = 2; i <= 10; i += 2) { count++; } System.out.println(count);
In a nested loop, what does the keyword break do when reached inside the inner loop?
What is the value of !(true || false)?
What does this print? int x = 7; if (x % 2 == 0) { System.out.print("even"); } else { System.out.print("odd"); }
What is printed by this nested loop? for (int i = 1; i <= 3; i++) { for (int j = 1; j <= i; j++) { System.out.print("*"); } System.out.println(); }
Which condition is true only when an int variable score is between 70 and 89 inclusive?
What does this print? int n = 5; int result = 1; while (n > 0) { result *= n; n--; } System.out.println(result);
What does continue do inside a for loop?
What is printed? int max = 0; int[] data = {3, 9, 2}; for (int v : data) { if (v > max) { max = v; } } System.out.println(max);
Which situation causes an infinite while loop?
What is the result of (4 < 4) || (3 <= 3)?
What is printed? for (int i = 5; i > 0; i--) { if (i == 3) { continue; } System.out.print(i); }
A sentinel-controlled loop continues reading values until a special terminating value is entered. Which loop structure is most appropriate?
What does this print? String s = "abc"; for (int i = 0; i < s.length(); i++) { System.out.print(s.substring(i, i + 1)); }
What value does count hold after this code? int count = 0; for (int i = 1; i <= 20; i++) { if (i % 3 == 0) { count++; } }
What is printed? int a = 4; int b = (a > 2) ? a * 2 : a + 2; System.out.println(b);
Why might initializing a maximum-finding variable to 0 produce a wrong answer?
What is printed? int x = 8; if (x > 10) System.out.print("big"); else if (x > 5) System.out.print("medium"); else System.out.print("small");
What does this print? int i = 1; int product = 1; do {} while (false); for (; i <= 3; i++) product *= 2; System.out.println(product);
Loading more…