Sail E0 Webinar

MCQs

Total Questions : 42 | Page 5 of 5 pages
Question 41.
public class While
{
public void loop()
{
int x= 0;
while ( 1 ) /* Line 6 */
{
System.out.print("x plus one is " + (x + 1)); /* Line 8 */
}
}
}


Which statement is true?


  1.    There is a syntax error on line 1.
  2.    There are syntax errors on lines 1 and 6.
  3.    There are syntax errors on lines 1, 6, and 8.
  4.    There is a syntax error on line 6.
 Discuss Question
Answer: Option D. -> There is a syntax error on line 6.

Using the integer 1 in the while statement, or any other looping or conditional 

construct for that matter, will result in a compiler error. This is old C Program 

syntax, not valid Java.

A, B and C are incorrect because line 1 is valid (Java is case sensitive so While 

is a valid class name). Line 8 is also valid because an equation may be placed 

in a String operation as shown.


Question 42.
switch(x)
{
default:
System.out.println("Hello");
}


Which two are acceptable types for x?



1. byte



2. long



3. char



4. float



5. Short



6. Long


  1.    1 and 3
  2.    2 and 4
  3.    3 and 5
  4.    4 and 6
 Discuss Question
Answer: Option A. -> 1 and 3

Switch statements are based on integer expressions and since both bytes and 

chars can implicitly be widened to an integer, these can also be used. Also shorts

 can be used. Short and Long are wrapper classes and reference types can not

 be used as variables.


Latest Videos

Latest Test Papers