WorksheetsJava switch/for/while
Total questions: 15
Worksheet time: 8mins
A SWITCH case statement in Java is a ___ control statement.
Iteration
Looping
Selection
Jump
Which is the alternative to SWITCH in Java language?
break, continue
for, while
if-else
goto, exit
What are the keywords used to implement a SWITCH case in Java language?
switch, case
default
break
All of above
A SWITCH statement accepts ___ type of data as input.
byte
short
int
String (case sensitive)
All of above
What is the output of Java program with SWITCH below?
int a=10;
switch(a)
{
case 2: System.out.println("TWO");
break;
case 10: System.out.println("TEN");
break;
}
No Output
TEN
Compiler error as there is no break
None
What is the output of the Java program below?
int b=20;
switch(b)
{
default: System.out.println("LION");
}
No Output
LION
Compiler error as there are no CASE statements.
None
What is the output of Java program with SWITCH?
int num=20;
switch(num)
{
case 10: System.out.println("TEN"); break;
case 20: System.out.println("TWENTY"); break;
case 30: System.out.println("THIRTY");
}
TEN
TWENTY
THIRTY
TEN TWENTY
What is the output of Java program below?
int num=40;
switch(num)
{
case 5: System.out.println("FIVE"); break;
case 35+5: System.out.println("FORTY"); break;
case 20+30: System.out.println("FIFTY");
}
FIVE
FORTY
FIFTY
Error
Single Line Comments are initiated with (a)
What is the output of Java program with SWITCH?
int num=20;
switch(num)
{
case 10: System.out.println("TEN"); break;
case 20: System.out.println("TWENTY");
case 30: System.out.println("THIRTY");
}
TEN
TWENTY
THIRTY
TEN
TWENTY
TWENTY
THIRTY
A SWITCH fall through (keeps going onto the next case) occurs in Java only in the absence of a ___.
Keyword "case"
Keyword "break"
Keyword "default"
None of these
Which way is a correct way to write an if statement in Java?
if (1 > 0)
{
System.out.println("1 is greater than 0");
}
if (1 > 0) ;
{
System.out.println("1 is greater than 0");
}
if (1 > 0)
[
System.out.println("1 is greater than 0");
]
If (1 > 0)
{
System.out.println("1 is greater than 0");
};
What does the below mean?
while(guess != 8)
Keep executing code if guess IS equal to 8
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,...
Keep executing code if guess IS NOT equal to 8
What code would print out the contents of array1 correctly?
String array1[] = {"Hulk", "Iron Man", "Spiderman", "Wolverine", "NightCrawler", "Cap Murica"};
for(int i = 0; i < array1.length; i++)
{
System.out.println(array1[i]);
}
int i = 0;
while(i<array1.length)
{
System.out.println(array1[i]);
i++;
}
for(int i = 0; i < 6; i++)
{
System.out.println(array1[i]);
}
int i = 0;
while(i < 6)
{
System.out.println(array1[i]);
i++;
}
What language do you prefer?
Java
Python
