|
1. Which of the following statements are printed after executing the given code? (Choose all the correct answers)
1. public class LoopTest {
2. public static void main(String[] args) {
3. new LoopTest().confusingIfs();
4. }
5.
6. void confusingIfs() {
7. int p = 9, q = 4, r = 7, s = 0;
8. if (r < q) {
9. if (s == 0) {
10. System.out.println(p);
11. } else if (p > q) {
12. System.out.println(q);
13. }
14. } else if (p > r) {
15. System.out.println(r);
16. } else {
17. System.out.println(s);
18. }
19. }
20. }
G. 9
H. 4
I. 7
J. 0
K. None of the above
2. Which of the following is true about the given method?
5. void test() {
6. final byte b = 3;
7. int i = 4;
8. while (i < 6) {
9. switch (i) {
10. case b : System.out.print("0 ");
11. default: System.out.print("none ");
12. case b + 1 : System.out.print("1 ");break;
13. case b - 2 : System.out.print("2 ");
14. }
15. i++;
16. }
17. }
A. The method will not compile because case statements for (b + 1) and (b - 2) are invalid.
B. The method will compile and prints “1 none 1” when executed.
C. The method will compile and prints “none ” when executed.
D. The method will compile and prints “0 none 1 2” when executed.
E. The method will compile and prints “2 none 1” when executed.
F. The method will compile and but will not print anything after execution.
3. How many times “Message” will be printed when the following code fragment is executed?
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
for (int k = 0; k < 5; k++) {
if (k == 4)
System.out.println("Message");
break;
}
}
}
A. 16
B. 64
C. 5
D. 1
E. 0
4. Which of the following are valid iterative statements? (Choose all the correct answers)
A. for (int j = 0; j < 5; System.out.println("here" + j),j++) {}
B. while(int i, i < 8) {
System.out.println("message");
i++;
}
C. int i=0;
do {
System.out.println("message");
} while( (i-1) != 8);
D. boolean i = false;
while(i)
System.out.println("message");
E. while(int i < 4) {
i++;
System.out.println("message");
}
5. Consider the given exception hierarchy. Assume that everything executed without any problem until line 5. What will be printed if UnknownError is thrown at line 6? (Choose all the correct answers)
Throwable
/ \
Error Exception
/ \
UnknownError IOException
/ \
FileNotFoundException InterruptedIOException
1. try {
2. FileInputStream myFile =
3. new FileInputStream("hello.txt");
4. myFile.read();
5. myFile.read();
6. myFile.read();
7. System.out.println("success");
8. } catch (FileNotFoundException fnfExp) {
9. System.out.println("file not found");
10. } catch (EOFException eofExp) {
11. System.out.println("End of file");
12. } catch (IOException ioExp) {
13. System.out.println("I/O operation failed");
14. } finally {
15. System.out.println("Cleaning up");
16. }
17. System.out.println("Continue...");
A. success
B. file not found
C. End of file
D. I/O operation failed
E. Cleaning up
F. Continue...
G. Nothing is printed
6. Consider the exception hierarchy and code sample in the previous question. Assume that everything executed without any problem until the line 5 and InterruptedIOException is thrown at line 6. Which of the following will be printed?
A. success
B. file not found
C. End of file
D. I/O operation failed
E. Cleaning up
F. Continue...
G. Nothing is printed
7. Which of the following statements is true about assertions?
A. You must handle the assertion failures using a catch clause.
B. In an assert statement, the expression before the : (colon) can be any Java expression with a value.
C. In an assert statement, the expression after the : (colon) can be any Java expression with a value.
D. If a switch block has no default statement, then it is appropriate to add a default statement with assert false;.
E. Sometimes it is appropriate to throw an error AssertionError instead of using the assert statement.
8. Which two of the following statements are true? (Choose two correct answers)
A. It is always a good practice to throw an AssertionError with throw instead of using the assert statement.
B. It is appropriate to place assertions where you think execution should never reach.
C. Public methods should not use assertions to verify their arguments.
D. It is a good practice to call a method doing significant things in the expression after the : (colon)
E. If an AssertionError is thrown in a try-catch block, the finally block will be never be executed.
9. Consider the given program. Which of the following statements is true about it?
1. package com.example.flowcontrol;
2.
3. public class AssertionTest {
4. public int problemValue;
5.
6. public void doSomething(int a) {
7. System.out.print("doSomething ");
8. problemValue = a;
9. }
10. public int getProblemValue(int b) {
11. System.out.print("get problem code ");
12. problemValue = b;
13. return problemValue;
14. }
15.
16. public void testingStuff(int importantVariable) {
17. int i = importantVariable;
18. assert (i != 0) : doSomething(i);
19. assert (i < 0) : getProblemValue(i);
20. }
21.
22. public static void main(String[] args) {
23. AssertionTest instance = new AssertionTest();
24. instance.testingStuff(-90);
25. }
26. }
A. The program will compile, but print nothing when executed.
B. The program will compile and print doSomething.
C. The program will compile and print get problem code.
D. The program will fail to compile at line 18.
E. The program will fail to compile at line 19.
F. The program will fail to compile at line 24.