[go: up one dir, main page]

0% found this document useful (0 votes)
52 views8 pages

JAVA MCQs

The document contains a series of multiple-choice questions and answers related to Java programming concepts. Key topics include Java keywords, data types, exception handling, arrays, and method signatures. Each question is followed by an explanation of the correct answer.

Uploaded by

daddalavicky90
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views8 pages

JAVA MCQs

The document contains a series of multiple-choice questions and answers related to Java programming concepts. Key topics include Java keywords, data types, exception handling, arrays, and method signatures. Each question is followed by an explanation of the correct answer.

Uploaded by

daddalavicky90
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

1. Which of the following is not a Java keyword?

A) class
B) interface
C) extends
D) implement

Answer: D) implement
Explanation: implement is incorrect; the correct keyword is implements.

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

A) 0
B) null
C) Depends on data type
D) No default value

Answer: D) No default value


Explanation: Local variables must be initialized before use; they don’t have default values.

3. Which method is the entry point of any Java program?

A) start()
B) run()
C) main()
D) init()

Answer: C) main()
Explanation: The main() method is the starting point: public static void main(String[] args).

4. What will happen if you try to compile and run the following code?

int x = 5;

int y = 0;

System.out.println(x / y);

A) 0
B) Compilation error
C) ArithmeticException
D) Infinity

Answer: C) ArithmeticException
Explanation: Division by zero in integers causes a runtime ArithmeticException.

5. Which of these is a valid constructor for a class named Book?


A) void Book() {}
B) Book(void) {}
C) Book() {}
D) constructor Book() {}

Answer: C) Book() {}
Explanation: Constructors have no return type and must have the same name as the class.

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

A) ArrayList
B) Vector
C) HashMap
D) LinkedList

Answer: A) ArrayList
Explanation: ArrayList is not synchronized and allows dynamic resizing with indexed access.

7. What is the size of an int variable in Java?

A) 8 bits
B) 16 bits
C) 32 bits
D) 64 bits

Answer: C) 32 bits
Explanation: Java int is always 32 bits (4 bytes), regardless of platform.

8. Which keyword is used to inherit a class in Java?

A) this
B) super
C) import
D) extends

Answer: D) extends
Explanation: extends is used for class inheritance in Java.

9. What is the output of the following code?

String s = "hello";

System.out.println(s.toUpperCase());

A) HELLO
B) hello
C) Hello
D) Compilation error

Answer: A) HELLO
Explanation: toUpperCase() converts all characters to uppercase.

10. Which of the following is used to handle exceptions in Java?

A) try-catch
B) throw
C) throws
D) All of the above

Answer: D) All of the above


Explanation: All are related: try-catch handles, throw generates, throws declares exceptions.

11. Which of these is not a primitive data type in Java?

A) byte
B) boolean
C) String
D) char

Answer: C) String
Explanation: String is a class, not a primitive type.

12. Java is a:

A) Compiled language
B) Interpreted language
C) Both A and B
D) Neither A nor B

Answer: C) Both A and B


Explanation: Java code is compiled to bytecode and interpreted by the JVM.

13. Which operator is used to compare two values for equality in Java?

A) =
B) :=
C) ==
D) equals

Answer: C) ==
Explanation: == compares primitive values; for objects, use equals().

14. What will the following code print?

System.out.println(10 + 20 + "30");
A) 3030
B) 102030
C) 3030
D) 3030

Answer: A) 3030
Explanation: 10 + 20 is 30, then "30" is appended as string: "3030".

15. Which package contains the Scanner class?

A) java.util
B) java.io
C) java.lang
D) java.net

Answer: A) java.util
Explanation: Scanner is in the java.util package.

16. Which keyword prevents inheritance of a class in Java?

A) static
B) abstract
C) final
D) private

Answer: C) final
Explanation: A class declared final cannot be extended.

17. Which interface must be implemented to create a thread in Java?

A) Runnable
B) Thread
C) Callable
D) EventListener

Answer: A) Runnable
Explanation: Implementing Runnable and passing it to a Thread is the standard way.

18. Which of the following is true about Java arrays?

A) Arrays are objects


B) Arrays can be resized
C) Arrays start at index 1
D) Arrays can contain different data types

Answer: A) Arrays are objects


Explanation: Arrays are objects and have a .length property.
19. What is the superclass of all classes in Java?

A) Object
B) Class
C) System
D) Main

Answer: A) Object
Explanation: Every Java class inherits from java.lang.Object directly or indirectly.

20. What is the result of this code?

int[] arr = new int[5];

System.out.println(arr[2]);

A) 0
B) null
C) Compilation error
D) ArrayIndexOutOfBoundsException

Answer: A) 0
Explanation: Arrays of primitives are initialized to default values; for int, it's 0.

21. What will be the output of the following code?

int a = 10;

int b = 5;

System.out.println(a++ - --b);

A) 6
B) 5
C) 4
D) 3

Answer: A) 6
Explanation:

• a++ returns 10 (then becomes 11)

• --b is 4

• So, 10 - 4 = 6
22. Which of the following is not a feature of Java?

A) Platform Independent
B) Pointers
C) Object-Oriented
D) Robust

Answer: B) Pointers
Explanation: Java doesn’t support pointers directly for safety and security reasons.

23. What is the output of the following Java code?

String s = "abc";

s.concat("def");

System.out.println(s);

A) abcdef
B) abc
C) def
D) Compilation Error

Answer: B) abc
Explanation: Strings in Java are immutable. concat() returns a new string, but result is not
stored.

24. What is the result of the following expression?

System.out.println(10 + 20 + "30" + 40);

A) 7030
B) 303040
C) 303040
D) 303040

Answer: A) 7030
Explanation:

• 10 + 20 = 30

• "30" becomes string

• "30" + 40 = "3040"

• Result: "30" + "3040" → "7030"


25. Which of the following allows duplicate elements?

A) HashSet
B) TreeSet
C) ArrayList
D) LinkedHashSet

Answer: C) ArrayList
Explanation: Only ArrayList allows duplicate elements; the others are sets (no duplicates).

26. Which exception is thrown when a thread is sleeping and interrupted?

A) InterruptedException
B) SleepException
C) ThreadException
D) IllegalThreadStateException

Answer: A) InterruptedException
Explanation: If a sleeping thread is interrupted, it throws InterruptedException.

27. Which of the following is the correct signature of the main method in Java?

A) public static void main(String args)


B) public static void Main(String[] args)
C) public static void main(String[] args)
D) static public void Main(String[] args)

Answer: C) public static void main(String[] args)


Explanation: This is the exact and required entry point signature.

28. What is method overloading in Java?

A) Changing the return type of a method


B) Using the same method name with different parameters
C) Writing the same method in a subclass
D) Redefining a method in a superclass

Answer: B) Using the same method name with different parameters


Explanation: Overloading = same method name, different parameter list.

29. Which of the following is not a valid access modifier in Java?


A) private
B) protected
C) friendly
D) public

Answer: C) friendly
Explanation: friendly is not a valid keyword. Default/package-private has no keyword.

30. What will the following code print?

int[] arr = {1, 2, 3, 4};

System.out.println(arr.length);

A) 3
B) 4
C) 5
D) Compilation Error

Answer: B) 4
Explanation: The array has 4 elements, so arr.length returns 4.

You might also like