TopicTestVariablesLoopsConditionalStatements
Test - Variables, Loops , Conditional Statements
Section A: Multiple Choice (1 mark each)
What is the correct way to declare a constant in Java? a.
var constantValue = 5;
b.final int constantValue = 5;
c.const constantValue = 5;
d.static final constantValue = 5;
Which data type is used to store text in Java? a.
int
b.char
c.String
d.double
What is the primary purpose of the
if
statement in Java? a. Loop control b. Variable declaration c. Conditional execution d. Exception handlingWhich loop is best suited for situations where you want to execute the loop body at least once, regardless of the condition? a.
for
loop b.while
loop c.do-while
loop d.if
loopIn Java, what is the result of the expression
5 % 2
? a. 3 b. 2.5 c. 2 d. 1In a
switch
statement, what happens if there is nobreak
statement after a case is executed? a. The program terminates. b. It causes a syntax error. c. Execution continues to the next case. d. The program jumps to the default case.Which of the following is used to compare two strings for content equality in Java? a.
==
b..equals()
c..equalsIgnoreCase()
d..compareTo()
What will be the value of
x
after the following code is executed?
javaint x = 10;
x += 5;
a. 5 b. 10 c. 15 d. Error
Which of the following is a valid declaration of a 2D integer array in Java? a.
int[] arr;
b.int[][] arr = new int[3][3];
c.int[][] arr = new int[];
d.int[3][3] arr;
Which statement is used to exit from a loop prematurely and continue with the next iteration? a.
break
b.exit
c.continue
d.return
Section B: True/False (1 mark each)
- True or False: In Java, the value of a
final
variable can be changed after it's initially assigned. - True or False: The
do-while
loop always executes its code block at least once. - True or False: The
else if
block in anif
statement is mandatory. - True or False: Java supports multiple inheritance through classes.
- True or False: The
char
data type in Java can store a Unicode character.
Section C: Code-related Questions (2 marks each)
Write a Java code snippet that declares an array of integers and initializes it with the values [1, 2, 3, 4, 5]. Then, use a
for
loop to print the sum of these values.Write a Java code snippet that calculates and prints the first 10 terms of the Fibonacci sequence using a
for
loop.Write a Java code snippet that takes a user's age as input and determines if they are eligible to vote. If the age is 18 or above, print "You are eligible to vote."
Write a Java code snippet that finds the maximum and minimum values in an array of integers.
Section D: Bonus (3 marks)
Explain the differences between the
for
,while
, anddo-while
loops in Java. Provide examples to illustrate these differences.Write a Java code snippet that checks whether a given string is a palindrome (reads the same forwards and backwards). Explain your code in comments.
Instructions:
- Answer all questions.
- You have 90 minutes to complete the test.
- Be concise and clear in your code-related answers.
- Good luck!
Comments
Post a Comment