Java OCA OCP Practice Question 1130

Question

What is the output of the following?

StringBuilder builder = new StringBuilder(); 
String str = new String("v"); 
do { 
        System.out.println(str); 
} while (builder); 
System.out.println(builder); 
  • A. v
  • B. This is an infinite loop.
  • C. The code does not compile.
  • D. The code compiles but throws an exception at runtime.


C.

Note

A do-while loop requires a boolean condition.

The builder variable is a StringBuilder and not a boolean.

The code does not compile, and Option C is correct.




PreviousNext

Related