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.
intb.charc.Stringd.doubleWhat is the primary purpose of the
ifstatement 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.
forloop b.whileloop c.do-whileloop d.ifloopIn Java, what is the result of the expression
5 % 2? a. 3 b. 2.5 c. 2 d. 1In a
switchstatement, what happens if there is nobreakstatement 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
xafter 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.
breakb.exitc.continued.return
Section B: True/False (1 mark each)
- True or False: In Java, the value of a
finalvariable can be changed after it's initially assigned. - True or False: The
do-whileloop always executes its code block at least once. - True or False: The
else ifblock in anifstatement is mandatory. - True or False: Java supports multiple inheritance through classes.
- True or False: The
chardata 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
forloop 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
forloop.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-whileloops 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