TopicTestVariablesLoopsConditionalStatements

 


Test - Variables, Loops , Conditional Statements

Section A: Multiple Choice (1 mark each)

  1. 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;

  2. Which data type is used to store text in Java? a. int b. char c. String d. double

  3. What is the primary purpose of the if statement in Java? a. Loop control b. Variable declaration c. Conditional execution d. Exception handling

  4. Which 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 loop

  5. In Java, what is the result of the expression 5 % 2? a. 3 b. 2.5 c. 2 d. 1

  6. In a switch statement, what happens if there is no break 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.

  7. Which of the following is used to compare two strings for content equality in Java? a. == b. .equals() c. .equalsIgnoreCase() d. .compareTo()

  8. What will be the value of x after the following code is executed?

java
int x = 10; x += 5;

a. 5 b. 10 c. 15 d. Error

  1. 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;

  2. 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)

  1. True or False: In Java, the value of a final variable can be changed after it's initially assigned.
  2. True or False: The do-while loop always executes its code block at least once.
  3. True or False: The else if block in an if statement is mandatory.
  4. True or False: Java supports multiple inheritance through classes.
  5. True or False: The char data type in Java can store a Unicode character.

Section C: Code-related Questions (2 marks each)

  1. 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.

  2. Write a Java code snippet that calculates and prints the first 10 terms of the Fibonacci sequence using a for loop.

  3. 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."

  4. Write a Java code snippet that finds the maximum and minimum values in an array of integers.

Section D: Bonus (3 marks)

  1. Explain the differences between the for, while, and do-while loops in Java. Provide examples to illustrate these differences.

  2. 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

Popular posts from this blog

FrontEnd - FAQs - Part 1

Java Script - FAQs

CoreJava - ClassesObjectsMethods - Assignment