Share
Explore

Java Test Questions

True or False: In Java, a variable must be declared before it can be used. [1]
Match the following Java concepts with their definitions: a. Object Oriented b. Methods c. Loops d. If-Then e. Variables i. A type of programming paradigm that uses "objects" containing data and methods. ii. Control flow structures that execute a block of code repeatedly based on a given condition. iii. Decision-making statements that allow the program to take different paths based on certain conditions. iv. Named storage location in memory. v. Blocks of code that perform specific tasks and can be invoked as needed. [1,2]
Multiple Choice: Which of the following is the correct syntax to declare a variable in Java? a. int 5 = x; b. x = int 5; c. int x = 5; d. 5 = int x; [1]
True or False: In an object-oriented programming language like Java, a method can be associated with an object. [1]
What will the output be for the following Java program if the input is 5?
Copy code public class Main { public static void main(String[] args) { int x = 5; if (x < 10) { System.out.println("Less than 10"); } else { System.out.println("Greater than or equal to 10"); } } } a. "Less than 10" b. "Greater than or equal to 10" c. No output d. Error [1,2]
True or False: The while loop in Java will continue to execute as long as the condition specified is true. [2]
Programming Exercise: Write the correct syntax to call a method named "display" from an object named "myObject" in Java. [1]
Multiple Choice: Which keyword is used to create a new object in Java? a. new b. create c. make d. generate [1]
True or False: In a Java "if-then" statement, if the condition is false, the program will execute the code inside the "else" block. [1,2]
Programming Exercise: Write a "for" loop in Java to print numbers from 1 to 10. [2]
Multiple Choice: In Java, what does the '==' operator check? a. Assignment b. Equality c. Inequality d. None of the above [1]
True or False: The method in Java is always a part of an object. [1]
Programming Exercise: Write a Java code snippet to declare a variable "x" of type double and assign it the value 5.5. [1]
Multiple Choice: Which of the following is a valid Java data type? a. int b. float c. string d. All of the above [1]
True or False: A Java method can take parameters. [1]
Programming Exercise: Write a Java "if-then-else" statement that checks whether a variable "n" is an even number. If it is, print "even". If it's not, print "odd". [1,2]
Multiple Choice: What will the following Java code print?
Copy code int i = 0; while (i < 5) { System.out.println(i); i++; } a. 0 1 2 3 4 5 b. 0 1 2 3 4 c. 1 2 3 4 5 d. 0 2 4 [2]
True or False: In Java, the same method name can be used in the same class as long as the number and/or types of parameters are different. This is known as method overloading. [1]
Programming Exercise: Write a Java code snippet that declares a variable "age" of type int and checks whether the age is greater than or equal to 18 using an "if-then" statement. [1,2]
Multiple Choice: In Java, which keyword is used to define a method? a. function b. method c. void d. define [1]
References: [1] Java Basics - Java Programming Tutorial [2] Java while loop with Examples [3] Java Basic Programming Exercises
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.