Q1. Given:
What would be the output, if it is executed as a program?
A. name =, pass =
B. name = null, pass = null
C. name = null, pass = false
D. name = null pass = true E. Compile error.
Answer: C
Explanation:
Both name and pass variables are instance variables, and we haven't given them any
values, so they take their default values. For Boolean default value is false and for string
which is not a primitive type default is null So at line 7, null will printed as the value of the
variable name, and at line 8 false will be printed. Hence Option C is correct.
As explained above options A, B and D are incorrect.
Code compiles fine so option E is incorrect.
Reference:
https://docs.oracle.com/javaseAutorial/java/javaOOAariables.html
Q2. Given the code fragment:
What is the result?
A. Jesse 25 Walter 52
B. Compilation fails only at line n1
C. Compilation fails only at line n2
D. Compilation fails at both line n1 and line n2
Answer: D
Q3. Given:
What is the result?
A. 97 98 99 100 null null null
B. 91 98 99 100 101 102 103
C. Compilation rails.
D. A NullPointerException is thrown at runtime.
E. An ArraylndexOutOfBoundsException is thrown at runtime.
Answer: A
Q4. Given:
What is the result?
A. Shining Sun Shining Sun Shining Sun
B. Shining Sun Twinkling Star Shining Sun
C. Compilation fails
D. A ClassCastException is thrown at runtime
Answer: D
Q5. Given:
What is the result?
A. int main 1
B. Object main 1
C. String main 1
D. Compilation fails
E. An exception is thrown at runtime
Answer: C
Q6. Given the fragments:
Which line causes a compilation error?
A. Line n1
B. Line n2
C. Line n3
D. Line n4
Answer: A
Q7. Given the code fragment:
Which modification enables the code to print 54321?
A. Replace line 6 with System, out. print (--x) ;
B. At line 7, insert x --;
C. Replace line 6 with --x; and, at line 7, insert system, out. print (x);
D. Replace line 12 With return (x > 0) ? false: true;
Answer: B
Q8. Given the code fragment:
What is the result?
A. 10 8 6 4 2 0
B. 10 8 6 4 2
C. AnArithmeticException is thrown at runtime
D. The program goes into an infinite loop outputting: 10 8 6 4 2 0. . .
E. Compilation fails
Answer: B
Q9. Given the code fragments:
Which code fragment, when inserted at line ni, enables the code to print Hank?
A. checkAge (iList, ( ) -> p. get Age ( ) > 40);
B. checkAge(iList, Person p -> p.getAge( ) > 40);
C. checkAge (iList, p -> p.getAge ( ) > 40);
D. checkAge(iList, (Person p) -> { p.getAge() > 40; });
Answer: C
Q10. Given the code fragment:
Which modification enables the code fragment to print TrueDone?
A. Replace line 5 With String result = "true"; Replace line 7 with case "true":
B. Replace line 5 with boolean opt = l; Replace line 7 with case 1=
C. At line 9, remove the break statement.
D. Remove the default section.
Answer: A
Q11. A method is declared to take three arguments. A program calls this method and passes only two arguments. What is the results?
A. Compilation fails.
B. The third argument is given the value null.
C. The third argument is given the value void.
D. The third argument is given the value zero.
E. The third argument is given the appropriate falsy value for its declared type. F) An
exception occurs when the method attempts to access the third argument.
Answer: A
Q12. Given:
What is the result?
A. x: 1 y: 2
B. 3 y: 4
C. x: 0 y: 0
D. 3 y: 4
E. x: 1 y: 2
F. 0 y: 0
G. x: 0 y: 0
H. 0 y: 0
Answer: C
Q13. What is the proper way to defined a method that take two int values and returns their sum as an int value?
A. int sum(int first, int second) { first + second; }
B. int sum(int first, second) { return first + second; }
C. sum(int first, int second) { return first + second; }
D. int sum(int first, int second) { return first + second; }
E. void sum (int first, int second) { return first + second; }
Answer: D
Q14. Given the code fragment:
What is the result?
A. 20
B. 25
C. 29
D. Compilation fails
E. AnArrayIndexOutOfBoundsException is thrown at runtime
Answer: A
Q15. Given:
public class Test {
static boolean bVar;
public static void main(String[] args) {
boolean bVar1 = true;
int count =8;
do {
System.out.println("Hello Java! " +count);
if (count >= 7) {
bVar1 = false;
}
} while (bVar != bVar1 && count > 4);
count -= 2;
}
}
What is the result?
A. Hello Java! 8 Hello Java! 6 Hello Java! 4
B. Hello Java! 8 Hello Java! 6
C. Hello Java! 8
D. Compilation fails
Answer: C
Explanation: Hello Java! 8