Looking For Anything Specific?

Java MCQs

1. Which of the following is the correct way to declare a constant in Java?


a) const int x = 5;


b) final int x = 5;


c) static final x = 5;


d) int final x = 5;

Answer: b) final int x = 5;



2. Which operator is used to concatenate strings in Java?


a) +


b) &


c) *


d) .

Answer: a) +



3. What is the default value of an object reference variable in Java?


a) null


b) 0


c) ""


d) undefined

Answer: a) null



4. Which method is used to get the length of an array in Java?


a) length()


b) getLength()


c) size()


d) length

Answer: d) length



5. Which keyword is used to prevent a method from being overridden in Java?


a) static


b) final


c) private


d) protected

Answer: b) final



6. What is the output of the following Java code: System.out.println(3 + 4 + "Java");?


a) 7Java


b) Java7


c) 34Java


d) Java34

Answer: a) 7Java



7. Which of the following is the correct way to declare an array of integers in Java?


a) int[] arr;


b) int arr[];


c) int[] arr = new int[10];


d) All of the above

Answer: d) All of the above



8. Which of the following is used to define a package in Java?


a) package;


b) import;


c) namespace;


d) package packageName;

Answer: d) package packageName;



9. Which of these methods is used to read a line of text from the console in Java?


a) Scanner.readLine()


b) Console.read()


c) BufferedReader.readLine()


d) System.in.read()

Answer: c) BufferedReader.readLine()



10. What does the this keyword refer to in Java?


a) The parent class


b) The current object


c) A static method


d) A new instance of a class

Answer: b) The current object



11. Which of these methods is used to get the value of a string in Java?


a) getString()


b) valueOf()


c) toString()


d) getValue()

Answer: b) valueOf()



12. Which of these is the correct way to handle exceptions in Java?


a) try-catch-finally


b) try-catch


c) catch-throw


d) throw-try

Answer: a) try-catch-finally



13. Which of these is used to create a thread in Java?


a) Thread.run()


b) Thread.start()


c) Thread.create()


d) Thread.invoke()

Answer: b) Thread.start()



14. What is the output of the following Java code: System.out.println(10 / 3);?


a) 3


b) 3.0


c) 3.3333


d) Error

Answer: a) 3



15. Which of the following is true about the String class in Java?


a) Strings are mutable


b) Strings are immutable


c) Strings can only be created using the new keyword


d) Strings cannot be concatenated

Answer: b) Strings are immutable



Post a Comment

0 Comments