What is the main purpose of the Java programming language?
A. To create web pages
B. To develop mobile applications
C. To enable platform-independent programming
D. To manage database systems
Answer: C
Explanation: Java is designed to be platform-independent, allowing developers to write code that can run on any device with a Java Virtual Machine (JVM).
Question 02
What is a Java class?
A. A collection of methods
B. A blueprint for creating objects
C. A type of variable
D. A data structure
Answer: B
Explanation: A Java class serves as a blueprint for creating objects, defining their properties and behaviors.
Question 03
Which of the following is a valid way to declare an integer variable in Java?
A. int 1number;
B. int number;
C. integer number;
D. int number:
Answer: B
Explanation: The correct syntax to declare an integer variable is int number;.
Question 04
What is the output of the following code snippet? System.out.println(5 + 3);
A. 5 + 3
B. 8
C. 53
D. Error
Answer: B
Explanation: The code outputs the result of the addition, which is 8.
Question 05
What does the static keyword indicate in Java?
A. The method can be overridden
B. The method belongs to the class, not instances
C. The variable can change
D. The method is private
Answer: B
Explanation: The static keyword indicates that the method or variable belongs to the class rather than instances of the class.
Question 06
Which of the following keywords is used to create a new object in Java?
A. create
B. new
C. make
D. instance
Answer: B
Explanation: The new keyword is used to create a new object in Java.
Question 07
What is the default value of a boolean variable in Java?
A. true
B. false
C. 0
D. null
Answer: B
Explanation: The default value of a boolean variable is false.
Question 08
How do you declare a constant variable in Java?
A. const int MAX_VALUE = 100;
B. final int MAX_VALUE = 100;
C. int const MAX_VALUE = 100;
D. MAX_VALUE int = 100;
Answer: B
Explanation: The final keyword is used to declare a constant variable in Java.
Question 09
What is the output of the following code snippet? System.out.println(“Hello, ” + “World!”);
A. Hello,
B. World!
C. Hello, World!
D. Error
Answer: C
Explanation: The code concatenates the two strings and outputs "Hello, World!".
Question 10
Which of the following is NOT a primitive data type in Java?
A. int
B. String
C. boolean
D. char
Answer: B
Explanation: String is not a primitive data type; it is a reference type in Java.
Question 11
What will happen if you try to access an array index that is out of bounds in Java?
A. It returns null
B. It crashes the program
C. It throws an ArrayIndexOutOfBoundsException
D. It returns 0
Answer: C
Explanation: Accessing an out-of-bounds index in an array will throw an ArrayIndexOutOfBoundsException.
Question 12
How do you define a method in Java?
A. method name() { … }
B. void method name() { … }
C. public void methodName() { … }
D. function methodName() { … }
Answer: C
Explanation: A method is defined using the syntax public void methodName() { ... }, specifying its access level and return type.
Question 13
Which of the following is a valid way to declare a character variable?
A. char letter = “A”;
B. char letter = ‘A’;
C. char letter = A;
D. letter char = ‘A’;
Answer: B
Explanation: A character variable is correctly declared using single quotes, as in char letter = 'A';.
Question 14
What does the return statement do in a method?
A. Exits the method and returns a value
B. Declares the method’s return type
C. Loops back to the start of the method
D. Displays the return type
Answer: A
Explanation: The return statement exits the method and optionally provides a value back to the caller.
Question 15
Which of the following operators is used for logical AND in Java?
A. &
B. &&
C. |
D. ||
Answer: B
Explanation: The && operator is used for logical AND operations in Java.
Question 16
How do you comment a single line in Java?
A. /* comment */
B. // comment
C. # comment
D. — comment
Answer: B
Explanation: Single-line comments in Java are made using //.
Question 17
Which statement is true regarding constructors in Java?
A. Constructors must have a return type.
B. A class can have multiple constructors (overloading).
C. Constructors can be private.
D. Both B and C are correct.
Answer: D
Explanation: Constructors can be overloaded, and they can also be declared private.
Question 18
What is an object in Java?
A. A data type
B. A variable
C. An instance of a class
D. A method
Answer: C
Explanation: An object is an instance of a class that encapsulates data and behavior.
Question 19
How do you declare an array of integers in Java?
A. int array[];
B. int[] array;
C. array int[];
D. Both A and B
Answer: D
Explanation: Both int array[]; and int[] array; are valid ways to declare an array of integers.
Question 20
Which of the following methods can be used to find the length of a string?
A. string.length()
B. string.size()
C. string.getLength()
D. string.length
Answer: A
Explanation: The length() method returns the number of characters in a string.