Java, one of the most widely used programming languages, offers a versatile platform for developing a range of applications, from web to mobile to enterprise solutions. As beginners embark on their journey to master Java, understanding its basics becomes paramount. To aid in this educational pursuit, we present a comprehensive set of 75+ multiple-choice questions (MCQs) covering the fundamental concepts of Java programming. These questions span topics such as data types, control structures, object-oriented programming (OOP) principles, and much more. Accompanied by correct answers and concise explanations, this collection aims to reinforce learning and clarify common misconceptions, providing a solid foundation in Java basics.

Advertisement

Q1 What is the correct syntax for declaring a new array in Java?


Correct Answer: A

The correct syntax for declaring a new array in Java is to use square brackets after the data type and before the variable name, like this: int[] myArray = {1, 2, 3, 4};

Q2 Which of the following is not a primitive data type in Java?


Correct Answer: D

Explanation: String is not a primitive data type in Java. It is a class in the java.lang package that represents a sequence of characters. The primitive data types in Java are: boolean, char, byte, short, int, long, float, and double.

Q3 What is the output of the following Java code?


int x = 5;
int y = 10;
int z = x + y;
System.out.println(z);


Correct Answer: C

Explanation: The code declares three variables: x, y, and z. x is assigned the value 5, y is assigned the value 10, and z is assigned the sum of x and y, which is 15. The final line of code prints the value of z to the console, so the output will be 15.

Q4 What is the purpose of the “final” keyword in Java?


Correct Answer: D

Explanation: The “final” keyword can be used in three different ways in Java:

When applied to a variable, it indicates that the variable is a constant and cannot be changed.
When applied to a method, it indicates that the method cannot be overridden by a subclass.
When applied to a class, it indicates that the class cannot be subclassed.

Q5 Which of the following is not a valid looping construct in Java?


Correct Answer: D

Explanation: The only looping constructs in Java are for, while, and do-while. “Repeat” is not a valid looping construct in Java.

Q6 What is the output of the following Java code?


int x = 10;
int y = 20;
if (x > y) {
  System.out.println("x is greater than y");
} else if (x 


Correct Answer: B

Explanation: The code compares the values of x and y using an if-else statement. Since x has a value of 10 and y has a value

Q7 Which of the following data types is used to store a single character in Java?


Correct Answer: D

Description: The char data type in Java is used to store a single character. It is a 16-bit Unicode character.

Q8 What does JVM stand for?


Correct Answer: A

Java Virtual Machine - The Java Virtual Machine (JVM) is a virtual machine that enables a computer to run Java programs as well as programs written in other languages that are also compiled to Java bytecode.

Q9 Which keyword is used to create a class in Java?


Correct Answer: A

class - The keyword class is used to define a class in Java.

Q10 Which method is called when a Java program starts?


Correct Answer: D

main() - The main() method is the entry point of any Java program. It is always executed when the program starts.

Q11 What is the size of a char in Java?


Correct Answer: B

16 bits - In Java, the char data type is 16 bits (2 bytes) in size, allowing it to represent Unicode characters.

Q12 Which of the following is a valid declaration of a String?


Correct Answer: A

String str = "Hello"; - This is the correct way to declare and initialize a String variable in Java.

Q13 Which operator is used to compare two values?


Correct Answer: B

The == operator is used to compare two primitive data values for equality.

Q14 What is the result of the expression 9 % 2?


Correct Answer: C

The result of 9 % 2 is 1, as % is the modulo operator that returns the remainder of the division.

Q15 Which of the following is a loop construct in Java?


Correct Answer: D

Both A and C - Both do-while and for are loop constructs in Java. do-while executes at least once, while for is used for a predetermined number of iterations.

Q16 What is encapsulation in Java?


Correct Answer: D

Both A and C - Encapsulation is the concept of wrapping data (variables) and code (methods) together as a single unit and hiding the internal state of an object from the outside. This ensures better control and security.

Q17 Which keyword is used to inherit a class in Java?


Correct Answer: A

The extends keyword is used for inheritance in Java, indicating that a class is deriving from a superclass.

Q18 How many constructors can a class have in Java?


Correct Answer: C

A class in Java can have multiple constructors as long as their parameter lists are different, known as constructor overloading.

Q19 Which of these access specifiers can be used for an interface?


Correct Answer: A

Interfaces in Java are implicitly abstract and can only be public.

Q20 What is the default value of a local variable in Java?


Correct Answer: D

Local variables in Java do not have a default value and must be initialized before use.

Q21 What is the output of?


System.out.println(10+20+"Hello"+10+20);


Correct Answer: A

30Hello1020 - In Java, evaluation is from left to right. The expression is evaluated as String concatenation after the first numerical addition.

Q22 Which keyword is used to access the superclass in Java?


Correct Answer: B

The super keyword in Java is used to refer to the immediate parent class object.

Q23 What does the expression evaluate to?


boolean isTrue = !false;


Correct Answer: C

The expression !false evaluates to true, as ! is the logical NOT operator.

Q24 What is polymorphism in Java?


Correct Answer: A

The ability of a variable, function or object to take on multiple forms. - Polymorphism in Java allows methods to do different things based on the object that it is acting upon, even though they share the same name.

Q25 What will be the output of the following Java code snippet?


int a = 10;
int b = ++a + a++ + --a + a--;
System.out.println(b);


Correct Answer: B

43 - The pre and post-increment/decrement operators change the value of a during the calculation.

Q26 Which collection class allows you to grow or shrink its size and provides indexed access to its elements, but is not synchronized?


Correct Answer: C

ArrayList allows dynamic resizing and provides indexed access to its elements but is not synchronized, making it unsuitable for thread-safe operations without external synchronization.

Q27 What will be the result of compiling and running the following Java code snippet?


public class Test {
    public static void main(String[] args) {
        int x = 10;
        int y = new Test().change(x);
        System.out.print(x + y);
    }
    int change(int x) {
        x = 12;
        return x;
    }
}


Correct Answer: B

22 - The x value is passed by value, so changes in the change method do not affect the original x. The method returns 12, which is added to the original x value for printing.

Q28 Which of the following is true about a final class in Java?


Correct Answer: C

It cannot be extended. - A final class in Java cannot be subclassed. This is often done for security reasons or to prevent the class from being subclassed.

Q29 Which interface does java.util.Hashtable implement?


Correct Answer: A

Map - java.util.Hashtable implements the Map interface, providing key-value storage.

Q30 What is the default value of a static variable of type int?


Correct Answer: A
Static integer variables in Java are automatically initialized to 0 if not explicitly initialized.

Q31 Which method must be implemented by all threads in Java?


Correct Answer: B
The run() method contains the code that constitutes a new thread. Any class that implements Runnable or extends Thread must override this method.

Q32 Which of these is used to make an object of a class serializable in Java?


Correct Answer: A
The Serializable interface in Java is a marker interface (with no methods) that objects must implement to be serialized or deserialized.

Q33 Which keyword is used to prevent method overriding in Java?


Correct Answer: A
Marking a method as final prevents it from being overridden in subclasses.

Q34 How can you access the first element of an array named arr?


Correct Answer: B
Arrays in Java are zero-indexed, meaning the first element is accessed with the index 0.

Q35 Which of these is a valid Java identifier?


Correct Answer: C
$hello is a valid identifier in Java. Identifiers can begin with a letter, the dollar sign "$", or an underscore "_".

Q36 Which class in Java is used for parsing strings to numbers?


Correct Answer: C
The Integer class (among others like Float, Double, etc.) provides methods to parse strings to their respective numeric types.

Q37 What is the size of an int in Java?


Correct Answer: C
The int data type in Java is 32 bits in size, allowing values from -2,147,483,648 to 2,147,483,647.

Q38 Which exception is thrown when trying to access a class for which a definition is not found?


Correct Answer: D
Both ClassNotFoundException and NoClassDefFoundError could be thrown, depending on the circumstance. ClassNotFoundException is a checked exception thrown when an application tries to load a class through its string name but no definition for the class with the specified name could be found. NoClassDefFoundError is an error that occurs when the class was available during compile time but not found in the Java classpath during runtime.

Q39 In Java, which operator is used for instance comparison?


Correct Answer: C
The instanceof operator in Java is used to test whether the object is an instance of the specified type (class or subclass or interface).

Q40 What does the transient keyword in Java do?


Correct Answer: A
The transient keyword is used in serialization. If a field is marked as transient, it will not be serialized.

Q41 Which of the following is true about the switch statement in Java?


Correct Answer: B
The switch statement in Java works with the byte, short, int, enum types, and String objects, but not with long, float, or double.

Q42 What will this code print?


System.out.println('100');


Correct Answer: C
This will print a special character represented by the octal value 100 because backslash is an escape character in Java strings, and it starts octal literals.

Q43 Which of the following is not a wrapper class in Java?


Correct Answer: B
String is not a wrapper class. Wrapper classes provide a way to use primitive data types (int, char, etc.) as objects.

Q44 How do you ensure that a resource is automatically closed at the end of the statement block?


Correct Answer: C
The try-with-resources statement ensures that each resource is closed at the end of the statement. Any object that implements java.lang.AutoCloseable or java.io.Closeable can be used as a resource.

Q45 What is the purpose of the super keyword in Java?


Correct Answer: D
The super keyword in Java is used for all of the above purposes: to call the superclass version of a method, to access a method in a superclass, and to call the constructor of a superclass.

Q46 Which of these collections is ordered?


Correct Answer: B
TreeSet is ordered by natural ordering of its elements or by a Comparator provided at set creation time.

Q47 What will be the result of compiling and running the following code?


int a = null;
System.out.println(a);


Correct Answer: C
There will be a compilation error because null cannot be assigned to a primitive type like int.

Q48 Which of these access specifiers allows visibility only within the same package and subclasses?


Correct Answer: C
The protected access specifier allows visibility within the same package and all subclasses, whether they are in the same package or not.

Q49 In Java, how do you declare a variable that cannot be modified after it is initialized?


Correct Answer: C
The final keyword is used to declare constants in Java. A final variable can only be initialized once, either via an initializer or in the constructor.

Q50 What is autoboxing in Java?


Correct Answer: A
Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes.

Q51 Which of the following is true about the enum type in Java?


Correct Answer: D
All of the statements are true about the enum type in Java. enum provides a means to define a group of related constants, and they can be declared within or outside of a class.

Q52 What does the static keyword in Java indicate?


Correct Answer: A
The static keyword indicates that a particular field or method belongs to the class, rather than any instance of the class, making it shared among all instances.

Q53 Which of the following is not a Java control statement?


Correct Answer: C
include is not a control statement in Java. It is used in languages like C and C++ for including files but not in Java.

Q54 In Java, what is method overloading?


Correct Answer: A
Method overloading in Java occurs when multiple methods have the same name but different parameters (method signatures) within the same class. This allows methods to perform similar functions but with different input parameters.

Q55 In Java, which of the following is the correct way to declare a variable of type float?


Correct Answer: D
In Java, a float variable can be declared with a literal ending in 'f' or 'F'. Both 'float f = 1.0f;' and 'float f = 1F;' are correct.

Q56 Which of the following is true about the static keyword in Java?


Correct Answer: D
The static keyword in Java can be applied to blocks, methods, and variables, making them belong to the class rather than an instance of the class.

Q57 Which of the following loop constructs will run at least once?


Correct Answer: C
The do-while loop in Java is executed at least once because the condition is checked after the loop's code is executed.

Q58 What is the result of 10 >> 2 in Java?


Correct Answer: C
The '>>' operator in Java is the right shift operator, shifting the bits of the first operand to the right by the number specified by the second operand. For '10 >> 2', it shifts the bits of 10 (1010 in binary) two places to the right, resulting in 2 (10 in binary), which is equal to 5.

Q59 What is the correct syntax for defining an interface in Java?


Correct Answer: C
In Java, an interface is defined using the 'interface' keyword followed by the interface name and curly braces. For example, 'interface MyInterface {}'.

Q60 Which method is used to compare two strings for equality in Java?


Correct Answer: B
The 'equals()' method is used to compare two strings for equality in Java. It compares the content of the strings rather than their reference addresses.

Q61 In Java, which keyword is used to reference the current object?


Correct Answer: B
The 'this' keyword in Java is used within an instance method or a constructor to refer to the current object.

Q62 Which of these is not a visibility modifier in Java?


Correct Answer: A
'transient' is not a visibility modifier; it's a keyword used to indicate that a field should not be serialized.

Q63 What does the finally block in a try-catch structure do?


Correct Answer: C
The finally block in a try-catch structure is executed regardless of whether an exception is thrown or not, making it the perfect place to close resources or to perform cleanup operations.

Q64 What is the base class of all classes in Java?


Correct Answer: C
The Object class is the root of the class hierarchy in Java. Every class has Object as a superclass.

Q65 Which of these is a correct way to allocate memory to an array in Java?


Correct Answer: D
All the given options are correct ways to allocate memory to an array in Java. They represent different syntaxes to achieve the same result.

Q66 What does the abstract keyword mean in Java?


Correct Answer: D
In Java, the abstract keyword is used to declare a method that does not have an implementation (method body) and must be implemented by subclasses.

Q67 Which of the following is true about package in Java?


Correct Answer: D
Packages in Java are used to group related classes and interfaces, prevent naming conflicts, and control access. They can be imported into other classes and interfaces using the import statement.

Q68 What is the output of the following snippet?


String s1 = "Java";
String s2 = new String("Java");
System.out.println(s1 == s2);


Correct Answer: B
The output is 'false' because '==' compares reference addresses, and 's1' and 's2' refer to different objects in memory.

Q69 In Java, constructor overloading:


Correct Answer: B
Constructor overloading in Java means creating multiple constructors within the same class, each having a different parameter list.

Q70 Which of the following is not an OOP principle?


Correct Answer: C
Compilation is not an object-oriented programming (OOP) principle. Encapsulation, Inheritance, and Polymorphism are considered the core principles of OOP.

Q71 The continue statement in Java:


Correct Answer: B
The continue statement in Java skips the current iteration of a loop (for, while, or do-while) and proceeds to the next iteration.

Q72 What will be the output of the following code?


for(int i=0; i


Correct Answer: B
The break statement terminates the loop when 'i' equals 3. Therefore, the loop prints '0 1 2' before terminating.

Q73 An instance of which of the following can be changed after it is created?


Correct Answer: D
Both StringBuilder and StringBuffer instances can be modified after they are created, making them mutable, unlike String instances which are immutable.

Q74 What is the scope of package-level access modifier in Java?


Correct Answer: B
Package-level access (default access, no modifier) allows visibility within the same package only.

Q75 Which of these is not a Java feature?


Correct Answer: B
Direct use of pointers is not a feature of Java, aiming to improve security and simplicity by abstracting away from direct memory manipulation.

Q76 The finalize() method in Java is called:


Correct Answer: A
The finalize() method is called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

Q77 Which of these is a valid declaration of a byte array in Java?


Correct Answer: D
Both 'byte[] arr = new byte[5];' and 'byte arr[] = new byte[5];' are valid declarations of a byte array in Java. The third option is incorrect syntax.

Q78 Which keyword is used for synchronizing a block of code in Java?


Correct Answer: A
The synchronized keyword in Java is used to indicate that a method or block of code is to be synchronized in a multithreaded environment.

Q79 Which exception is thrown when accessing an out-of-bound index of an array in Java?


Correct Answer: A
ArrayIndexOutOfBoundsException is thrown to indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array.

Navigating through the basics of Java with these 50 MCQs offers learners an opportunity to test their knowledge and gain a deeper understanding of the language's core principles. Each question, along with its explanation, serves as a building block in the learning process, addressing key concepts from syntax to OOP. This resource is designed not only to challenge but also to enlighten, ensuring that beginners can progress in their Java programming journey with confidence. Whether you're preparing for an exam, brushing up on your skills, or just starting out, these questions can be a valuable tool in your study arsenal, helping to lay a strong foundation for more advanced Java programming topics.

Share.
Leave A Reply


Exit mobile version