Java is a versatile and widely-used programming language that forms the foundation of many software applications. Whether you’re a beginner or preparing for a Java certification exam, mastering the basics is crucial. In this collection of Java MCQs, we’ve covered fundamental topics such as data types, instance variables, object creation, and key concepts like the final and static keywords. These questions are designed not only to test your knowledge but also to reinforce your understanding of core Java principles.
From understanding default values of variables to correctly implementing object-oriented principles, these MCQs delve into essential areas that are frequently encountered in exams and interviews. By practicing these questions, you will gain a deeper understanding of Java’s syntax, memory management, and control flow mechanisms, all of which are vital for any Java developer.
Use this quiz to solidify your Java basics and prepare effectively for your exams. Each question is accompanied by a detailed explanation, ensuring you grasp the underlying concepts thoroughly. Whether you’re learning Java for the first time or brushing up on your skills, these MCQs will help you succeed.
1. What value is automatically assigned to an instance variable of type int
if it is not explicitly initialized in Java?
a. 0
b. null
c. -1
d. 1
2. Which of the following is a valid declaration of a `char` in Java?
a. `char c = ‘ab’;`
b. `char c = ‘a’;`
c. `char c = “a”;`
d. `char c = 97;`
3. What is the size of a `byte` data type in Java?
a. 8 bits
b. 16 bits
c. 32 bits
d. 64 bits
4. Which of the following is true about the `final` keyword in Java?
a. A `final` class can be inherited.
b. A `final` method cannot be overridden.
c. A `final` variable can be re-assigned.
d. `final` and `finally` are the same.
5. How do you correctly create an instance of a class named `MyClass` in Java?
a. `MyClass obj = MyClass();`
b. `MyClass obj = new MyClass();`
c. `MyClass obj = MyClass.new();`
d. `MyClass obj = new MyClass;`
6. Which of these statements about the `static` keyword is true in Java?
a. `static` methods can access instance variables directly.
b. `static` methods belong to the class rather than any object instance.
c. `static` variables cannot be shared among instances of a class.
d. A `static` method can be overridden by a non-static method.
7. What will be the output of the following code?
a. 4
b. 5
c. 6
d. Compilation error
8. Which of the following is the correct way to declare an array in Java?
a. `int arr[] = new int(10);`
b. `int arr[] = new int[10];`
c. `int arr[10];`
d. `int arr = new int[10];`
9. What is the purpose of the `break` statement in Java loops?
a. To skip the current iteration and move to the next one.
b. To exit from the outer loop.
c. To exit from the loop entirely.
d. To jump to a labeled statement outside the loop.
10. In Java, which of these access modifiers provides the most restrictive access?
a. private
b. protected
c. default
d. public
11.Which of the following methods is used to start a thread in Java?
a. run()
b. start()
c. init()
d. execute()
12. What is the correct way to create a new object in Java?
a. MyClass obj = MyClass();
b. MyClass obj = new MyClass;
c. MyClass obj = new MyClass();
d. MyClass obj = MyClass.new();
13. Which keyword is used to inherit a class in Java?
a. implements
b. extends
c. inherits
d. superclass
14. Which of the following is not a valid data type in Java?
a. int
b. float
c. boolean
d. number
15. What is the purpose of the static keyword in Java?
a. To create a constant variable
b. To create a method that belongs to an instance of a class
c. To create a method that belongs to a class rather than an instance
d. To declare a method that cannot be overridden
16. Which method is used to find the length of a string in Java?
a. size()
b. length()
c. getSize()
d. getLength()
17. In Java, which of the following operators is used to check if two values are equal?
a. =
b. ==
c. equals
d. !=
18. What is the default value of an int variable in Java?
a. 0
b. 1
c. null
d. undefined
19. Which of the following is a valid declaration of a float in Java?
a. float f = 1.2;
b. float f = 1.2f;
c. float f = 1.2d;
d. float f = “1.2”;
20. Which of the following loops will always execute the code block at least once?
a. for
b. while
c. do-while
d. foreach
21. What does the keyword void mean in a method declaration?
a. The method returns an int
b. The method returns a string
c. The method returns nothing
d. The method returns a boolean
22. In Java, how do you convert a string to an integer?
a. Integer.valueOf(string)
b. Integer.parseInt(string)
c. string.toInt()
d. string.parseInt()
23. What is the output of the following code?
System.out.println(10 + 20 + “30”);
a. 3030
b. 102030
c. 30
d. 3020
24. Which of the following is true about the finalize() method in Java?
a. It is used to finalize the execution of a program
b. It is called by the garbage collector before an object is destroyed
c. It must be called manually to free up memory
d. It is used to set the final value of a variable
25. Which of the following is a valid keyword in Java?
a. virtual
b. throws
c. event
d. include
26. In Java, what is the correct way to declare a variable of type double?
a. double x = 10;
b. Double x = 10;
c. double x = 10.0;
d. Double x = “10.0”;
27. What is the use of the break statement in Java?
a. To break out of a method
b. To break out of a loop or switch statement
c. To break out of the program
d. To break the flow of execution and skip to the next iteration
28. Which of the following is not a feature of Java?
a. Platform independent
b. Object-oriented
c. Pointers
d. Automatic garbage collection
29. What does the continue statement do in a loop?
a. It ends the loop
b. It skips the rest of the current iteration and jumps to the next iteration
c. It skips to the end of the loop
d. It jumps to a specified part of the loop
30. Which of the following is not an access modifier in Java?
a. public
b. private
c. protected
d. package
31. What is the correct syntax for a main method in Java?
a. public static void main()
b. public static void main(String[] args)
c. public void main(String[] args)
d. static void main(String args[])
32. In Java, what is the default value of a boolean variable?
a. true
b. false
c. null
d. 0
33. Which of the following statements is true about Java?
a. Java is a compiled language
b. Java is an interpreted language
c. Java is both compiled and interpreted
d. Java is neither compiled nor interpreted
34. Which of the following is used to create an object in Java?
a. new
b. create
c. instance
d. object
35. What is the output of the following code?
int x = 5;
System.out.println(++x);
a. 4
b. 5
c. 6
d. 7
36. Which of the following is not a primitive data type in Java?
a. int
b. char
c. String
d. boolean
37. What does the synchronized keyword do in Java?
a. It ensures that a method can only be accessed by one thread at a time
b. It ensures that multiple threads can access a method simultaneously
c. It synchronizes two or more methods
d. It allows multiple threads to modify a variable
38. Which of the following is not a valid keyword in Java?
a. interface
b. instanceof
c. friend
d. volatile
39. What is the correct way to declare an array in Java?
a. int[] arr = new int[5];
b. int arr[] = new int(5);
c. int arr[5] = new int[];
d. int arr = new int[5];
40. Which of the following is true about the super keyword in Java?
a. It refers to the current object
b. It is used to call the parent class constructor
c. It is used to refer to a method in the same class
d. It is used to create an object
41. In Java, which of the following is true about an interface?
a. An interface can contain abstract methods only
b. An interface can contain concrete methods only
c. An interface can contain both abstract and concrete methods
d. An interface cannot contain any methods
42. Which of the following is not a valid way to create a string in Java?
a. String s = “Hello”;
b. String s = new String(“Hello”);
c. String s = ‘Hello’;
d. String s = new String();
43. What is the result of the following code?
int x = 10;
x += 5;
System.out.println(x);
a. 10
b. 15
c. 5
d. 25
44. What does the throw keyword do in Java?
a. It throws an exception
b. It throws an error
c. It creates an exception
d. It creates an error
45. Which of the following is true about a constructor in Java?
a. It must have the same name as the class
b. It can have any name
c. It must return a value
d. It must be static
46. In Java, what is the correct way to convert a string to lowercase?
a. toLower()
b. toLowerCase()
c. lowerCase()
d. toLowerString()
47. What is the output of the following code?
String s = “Java”;
System.out.println(s.length());
a. 3
b. 4
c. 5
d. 6
48. Which of the following is true about a final variable in Java?
a. Its value can be changed
b. Its value cannot be changed
c. It must be declared at the time of use
d. It must be declared inside a method
49. In Java, which of the following statements is true about inheritance?
a. A subclass inherits all members of a superclass except constructors
b. A subclass inherits only the constructors of a superclass
c. A subclass inherits only the methods of a superclass
d. A subclass inherits only the fields of a superclass
50. Which of the following is a correct way to create a thread in Java?
a. By extending the Thread class
b. By implementing the Runnable interface
c. By using a lambda expression
d. All of the above
You may also like this :Unique Project Ideas for Final-Year Computer Science Students with Real-Time Uses
Answers:
Here are the answers with short explanations for each question:
1. Q.1) a
Explanation: The default value of an `int` instance variable in Java is `0`. This helps prevent the use of uninitialized variables.
2. Q.2) b
Explanation: The `char` data type in Java must be declared with a single character enclosed in single quotes, e.g., `char c = ‘a’;`.
3. Q.3) a
Explanation: The `byte` data type in Java is an 8-bit signed integer, which has a size of 8 bits.
4. Q.4) b
Explanation: The `final` keyword in Java prevents a method from being overridden by subclasses, ensuring that the method remains unchanged.
5. Q.5) b
Explanation: In Java, objects are created using the `new` keyword followed by the class constructor, e.g., `MyClass obj = new MyClass();`.
6. Q.6) b
Explanation: `static` methods and variables belong to the class itself rather than any specific instance, meaning they can be accessed without creating an object of the class.
7. Q.7) b
Explanation: The postfix increment operator (`x++`) increases the value of `x` after it is used in the expression, so the output is `5`.
8. Q.8) b
Explanation: The correct way to declare an array in Java is `int arr[] = new int[10];`, which allocates memory for 10 integers.
9. Q.9) c
Explanation: The `break` statement in Java is used to exit from the loop entirely, stopping further iterations.
10. Q.10) a
Explanation: The `private` access modifier is the most restrictive, allowing access only within the same class.
11. Q.51) b
Explanation: The `start()` method is used to begin the execution of a thread in Java. The `run()` method is called internally by the `start()` method.
12. Q.12) c
Explanation: The correct syntax to create a new object in Java is `MyClass obj = new MyClass();`.
13. Q.13) b
Explanation: The `extends` keyword is used in Java to inherit a class.
14. Q.14) d
Explanation: Java does not have a data type called `number`. The valid data types are `int`, `float`, `boolean`, etc.
15. Q.15) c
Explanation: The `static` keyword in Java is used to create methods or variables that belong to the class rather than an instance.
16. Q.16) b
Explanation: The `length()` method is used to find the length of a string in Java.
17. Q.17) b
Explanation: The `==` operator is used to check if two values are equal in Java.
18. Q.18) a
Explanation: The default value of an `int` variable in Java is `0`.
19. Q.19) b
Explanation: In Java, the correct way to declare a `float` is `float f = 1.2f;`, where the `f` suffix indicates a float literal.
20. Q.20) c
Explanation: The `do-while` loop in Java will always execute the code block at least once because the condition is checked after the loop body executes.
21. Q.21) c
Explanation: The `void` keyword in a method declaration means that the method does not return any value.
22. Q.22) b
Explanation: The `Integer.parseInt(string)` method is used to convert a string to an integer in Java.
23. Q.23) a
Explanation: The output of `System.out.println(10 + 20 + “30”);` is `3030` because the integers are added first, then concatenated with the string.
24. Q.24) b
Explanation: The `finalize()` method is called by the garbage collector before an object is destroyed in Java.
25. Q.25) b
Explanation: `throws` is a valid keyword in Java, used to declare an exception that a method might throw.
26. Q.26) c
Explanation: The correct way to declare a variable of type `double` in Java is `double x = 10.0;`.
27. Q.27) b
Explanation: The `break` statement in Java is used to exit a loop or switch statement immediately.
28. Q.28) c
Explanation: Java does not support `pointers`, which is a feature commonly associated with languages like C and C++.
29. Q.29) b
Explanation: The `continue` statement in a loop skips the rest of the current iteration and jumps to the next iteration.
30. Q.30) d
Explanation: `package` is not an access modifier in Java; the correct access modifiers are `public`, `private`, and `protected`.
31. Q.31) b
Explanation: The correct syntax for the main method in Java is `public static void main(String[] args)`.
32. Q.32) b
Explanation: The default value of a `boolean` variable in Java is `false`.
33. Q.33) c
Explanation: Java is both compiled and interpreted. Java code is compiled into bytecode, which is then interpreted by the Java Virtual Machine (JVM).
34. Q.34) a
Explanation: The “new” keyword is used in Java to create a new object.
35. Q.35) c
Explanation: The output of `System.out.println(++x);` when `x` is 5 will be `6` because the `++` operator increments the value before using it in the expression.
36. Q.36) c
Explanation: `String` is not a primitive data type in Java; it is an object.
37. Q.37) a
Explanation: The `synchronized` keyword ensures that a method can only be accessed by one thread at a time in Java.
38. Q.38) c
Explanation: `friend` is not a valid keyword in Java; it is used in C++. Valid Java keywords include `interface`, `instanceof`, and `volatile`.
39. Q.39) a
Explanation: The correct way to declare an array in Java is `int[] arr = new int[5];`.
40. Q.40) b
Explanation: The `super` keyword in Java is used to call the parent class constructor.
41. Q.41) a
Explanation: An interface in Java can contain abstract methods only, which must be implemented by the classes that implement the interface.
42. Q.42) c
Explanation: `’Hello’` is not a valid way to create a string in Java; strings must be enclosed in double quotes, not single quotes.
43. Q.43) b
Explanation: The output of `System.out.println(x);` after `x += 5;` when `x` is 10 will be `15`.
44. Q.44) a
Explanation: The `throw` keyword in Java is used to explicitly throw an exception.
45. Q.45) a
Explanation: A constructor in Java must have the same name as the class and does not have a return type.
46. Q.46) b
Explanation: The correct method to convert a string to lowercase in Java is `toLowerCase()`.
47. Q.47) b
Explanation: The `length()` method returns the number of characters in a string. The output for `System.out.println(s.length());` when `s = “Java”` will be `4`.
48. Q.48) b
Explanation: A `final` variable in Java is a constant, meaning its value cannot be changed once it has been assigned.
49. Q.49) a
Explanation: A subclass inherits all members of a superclass except constructors in Java.
50. Q.50) d
Explanation: All of the options (`extending the Thread class`, `implementing the Runnable interface`, and `using a lambda expression`) are correct ways to create a thread in Java.
Follow us on Instagram : know.programming
1 thought on “Essential Java Basics MCQ’s for Exam Preparation”