Java Control Statements 01

Quiz
•
Other
•
University
•
Hard
Sumit Mittu
Used 3+ times
FREE Resource
5 questions
Show all answers
1.
MULTIPLE CHOICE QUESTION
20 sec • 2 pts
Which of these is not a Control statement in Java?
? and :
if-else
switch
do-while
return
Answer explanation
The '? and :' operators are known as the ternary conditional operator in Java, not control statements. Control statements include 'if-else', 'switch', and 'do-while', which control the flow of execution.
2.
MULTIPLE CHOICE QUESTION
10 sec • 3 pts
Which of the following statements test only for equality?
if
switch
for
while
Answer explanation
We can use all relational operators for conditions with if, for and while but the switch statement compares only for equality condition of variable's value with case values.
3.
FILL IN THE BLANK QUESTION
20 sec • 2 pts
Fill in the blanks such that the following code must display "Hello" on the screen.
int x = 19;
if(x ___ 19) System.out.print("Hello"); else System.out.print("Bye");
Answer explanation
To display 'Hello', the condition must be true. Using '==' checks if x is equal to 19. Therefore, the correct choice is '==' which allows the code to print 'Hello' when x is 19.
4.
MULTIPLE CHOICE QUESTION
30 sec • 4 pts
Choose the most appropriate of the options that apply to the following code segment:
char section = 'C';
switch(section) {
default: System.out.println("Mix batch");
case 'A': System.out.println("Toppers"); break;
}
Compiler Error: the default case not allowed to be written before other cases.
Compiler error : Must use break after every case
Compiler error: Cannot use char variables as switch parameter
Output:
Mix batch
Toppers
Output:
Mix batch
Answer explanation
The switch statement evaluates 'section' as 'C', which does not match any case. It falls to the default case, printing 'Mix batch'. Due to missing break statement after the default case, the case 'A' will also be executed, so 'Toppers' is also printed. Thus, the output is:
Mix batch
Toppers
5.
MULTIPLE SELECT QUESTION
45 sec • 4 pts
Spot the location(s) reported for syntax error in the following code:
class Demo{
public static void main(String args[]) { //Line-1
int expense = 1_000; //Line-2
while(expense++ < 1100) ; //Line-3
{ boolean canSpendMore = true; return; } //Line-4
System.out.println(expense); //Line-5
} }
Tick all that apply.
Line-1
Line-2
Line-3
Line-4
Line-5
Answer explanation
Line-5 has a syntax error because it is outside of any method or block after the return statement in Line-4. The return statement exits the method, making any code after it unreachable.
Similar Resources on Wayground
10 questions
Java 1

Quiz
•
University
10 questions
Formative Question DET50083 Power System Protection

Quiz
•
University
10 questions
SSHE CONTRACTOR LEARNING WORKSHOP #2

Quiz
•
University
10 questions
Unidad II

Quiz
•
University
10 questions
3 phase AC system

Quiz
•
University
9 questions
Pompa injeksi inline

Quiz
•
11th Grade - University
10 questions
Kuis 2 Dasar Kulinari

Quiz
•
University
10 questions
Raspberry PI YEAR 5

Quiz
•
9th Grade - University
Popular Resources on Wayground
10 questions
Video Games

Quiz
•
6th - 12th Grade
20 questions
Brand Labels

Quiz
•
5th - 12th Grade
15 questions
Core 4 of Customer Service - Student Edition

Quiz
•
6th - 8th Grade
15 questions
What is Bullying?- Bullying Lesson Series 6-12

Lesson
•
11th Grade
25 questions
Multiplication Facts

Quiz
•
5th Grade
15 questions
Subtracting Integers

Quiz
•
7th Grade
22 questions
Adding Integers

Quiz
•
6th Grade
10 questions
Exploring Digital Citizenship Essentials

Interactive video
•
6th - 10th Grade
Discover more resources for Other
20 questions
Definite and Indefinite Articles in Spanish (Avancemos)

Quiz
•
8th Grade - University
7 questions
Force and Motion

Interactive video
•
4th Grade - University
36 questions
Unit 5 Key Terms

Quiz
•
11th Grade - University
7 questions
Figurative Language: Idioms, Similes, and Metaphors

Interactive video
•
4th Grade - University
15 questions
Properties of Equality

Quiz
•
8th Grade - University
38 questions
WH - Unit 3 Exam Review*

Quiz
•
10th Grade - University
21 questions
Advise vs. Advice

Quiz
•
6th Grade - University
12 questions
Reading a ruler!

Quiz
•
9th Grade - University