KnowledgeBoat Logo

Computer Applications

State the type of errors, if any in the following statements.

(a)

switch (x < 2)

(b)

int a = 100, b = 0;    
System.out.println (a / b);

Input in Java

7 Likes

Answer

(a) There is a syntax error in the switch statement. In a switch statement, the expression inside the parentheses should evaluate to an integer, a character, or an enumerated type. However, x < 2 is a boolean expression, and it cannot be used directly as the expression in a switch statement.

(b) The statement System.out.println (a / b); will result in a runtime error as the user is trying to divide a number by 0 which is not possible.

Answered By

2 Likes


Related Questions