Java MCQs from PYQs

Disclaimer: The answers provided are AI-generated and may contain inaccuracies. Please verify the answers before use.

1. Which provides a runtime environment for Java byte code to be executed?

Answer: b. JVM

2. Which of the following are not Java keywords?

Answer: c. then

3. The default value of a static integer variable of a class in Java is,

Answer: a. 0

4. Multiple inheritance means,

Answer: a. one class inheriting from more superclasses

5. Which statement is not true in the Java language?

Answer: b. A private member of a class cannot be accessed by the methods of the same class (False).

6. To prevent any method from overriding, we declare the method as,

Answer: c. final

7. Which one of the following is not true?

Answer: c. An abstract class cannot have non-abstract methods (False).

8. The fields in an interface are implicitly specified as,

Answer: d. both static and final

9. Which of the following is true?

Answer: c. A finally block is executed whether an exception is thrown or not.

10. What is garbage collection in the context of Java?

Answer: c. When all references to an object are gone, the memory used by the object is automatically reclaimed.

11. The Java runtime system automatically calls this method while garbage collection.

Answer: b. finalize()

12. The correct order of the declarations in a Java program is,

Answer: a. Package declaration, import statement, class declaration

13. An overloaded method consists of,

Answer: d. Both (a) and (b) above

14. A protected member can be accessed in, Which is the false option?

Answer: b. non-subclass of the same package (False).

15. All exception types are subclasses of the built-in class

Answer: d. Throwable

16. When an overridden method is called from within a subclass, it will always refer to the version of that method defined by the

Answer: b. Subclass

17. Java compiler javac translates Java source code into

Answer: b. Byte code

18. In Java, a character constant’s value is its integer value in the

Answer: b. Unicode

19. In Java, a try block should immediately be followed by one or more .................... blocks.

Answer: d. Catch

20. An abstract data type typically comprises a ............... and a set of .................. respectively.

Answer: d. Data representation, operations.

21. Which method can be defined only once in a program?

Answer: b. finalize method

22. Which concept of Java is a way of converting real world objects in terms of class?

Answer: c. Abstraction

23. Declaring a method final means:

Answer: d. It cannot be overridden.

24. Which of the following statements regarding static methods are correct?

Answer: c. 2 and 4

25. Which statements are correct for the given case?

Answer: c. UberTaxi.taxiBooking() is a valid invocation of taxiBooking()

26. Which of the following statements is/are true about constructor?

Answer: c. Only (i) and (iii) are true

27. Which of the following is true about the use of java access modifiers?

Answer: b. You cannot specify accessibility of local variables. They are only accessible within the block in which they are declared.

28. When will thread A become a candidate to get another turn to execute after calling wait(4000)?

Answer: b. After object B is notified, or after four seconds.

29. Which exception class could be used to produce a suitable catch block for Integer.parseInt("one")?

Answer: c. NumberFormatException

30. When compared java.io.BufferedWriter to java.io.FileWriter, which extra capability does exist in only one of the two as a method?

Answer: d. writing a line separator to the stream

31. Which of the following is/are true about packages in Java?

Answer: b. Only 1, 2 and 3

32. How to propagate the unchecked Exceptions?

Answer: d. Unchecked exceptions are automatically propagated

33. Which of the following is true about generics in Java?

Answer: d. All of the mentioned

34. Which collection class helps you to expand or shrink its size and gives its elements indexed access, but whose techniques are not synchronised?

Answer: c. java.util.ArrayList

35. Given that Thing is a class, how many objects and how many reference variables are created by the given code?

Answer: c. Two objects are created. Three reference variables are created.

36. Can data flow through a given stream in both directions?

Answer: b. No; a stream has one direction only, input or output.

37. What are valid statements for daemon threads?

Answer: d. All of above.

38. What is difference between starting thread with run() and start() method?

Answer: a. When you call start() method, main thread internally calls run() method to start newly created Thread.

39. What are the rules applicable to define multiple try, catch & finally blocks?

Answer: a. 1,2,3

40. An abstract data type typically comprises a ............... and a set of .................. respectively.

Answer: d. Data representation, operations

41. Which of the following primitive data types are not integer types? Select the two correct answers.

Answer: b) float, d) double

42. Which one of the following array declaration statements is not legal? Select the one correct answer.

Answer: c) int a[][] = new int [][4];

43. Given the following declaration: char c = 'A'; What is the simplest way to convert the character value in c into an int? Select the one correct answer.

Answer: a) int i = c;

44. Which of the following expressions evaluate to true? Select the two correct answers.

Answer: a) (false | true), c) (4 <= 4)

45. Which is true? (Choose all that apply.)

Answer: c) "X extends Y" is correct if X and Y are either both classes or both interfaces

46. Is this comment correct?

public class JavaComment{
    public static void main(String a[]){
        System.out.pri/* my comment line*/ntln("Is this comment correct?");
    }
}

Answer: Yes, the comment is correctly placed. The compiler ignores the text between /* and */.

47. A java interface can contain _______.

Answer: d. public static Final Variables and abstract methods both

48. Which class is used to create a thread in Java?

Answer: a. Thread

49. What happens when a constructor is defined for an interface?

Answer: a. Compilation failure

50. What happens when we access the same variable defined in two interfaces implemented by the same class?

Answer: a. The interfaceName.variableName needs to be defined

51. Suppose you are working in a Java project. You have a requirement where you need a dynamic or resizable array-like data structure that can allow you to add, remove, and access elements at a specific index efficiently. It should also automatically adjust its size as elements are added or removed. Which of the following class will you use to fulfil your requirement?

Answer: c) ArrayList

52. Which of these is a super class of all errors and exceptions in the Java language?

Answer: b. Throwable

53. What is the output of this program?

Answer cannot be determined without seeing the code.

54. File Class is inside which package?

Answer: d. java.io

55. Which method is used to reads one byte of data from the input stream?

Answer: c) read()

56. The input stream is used to read data from numerous input devices like the keyboard, network, etc.

Answer: a. TRUE

57. Scope of the local variable is_________

Answer: b) Within the Method or block

58. What is the difference between constructor method and method?

Answer: a) Constructor will be automatically invoked when an object is created. Whereas method has to be call explicitly.

59. When a string literal is used in the program, Java automatically creates instances of the string class.

Answer: a) True

60. What is the result of expression 5.45 + "3.2"?

Answer: d) The String "5.453.2"

61. What is an example of polymorphism?

Answer: c) Method overloading

62. What is the result of executing the following code when the value of x is 2:

Cannot be determined without seeing the code.

63. What is the effect of compiling and (if possible) running this class:

class Example {
    public static void main(String[] args) {
        // Example code here
    }
}

Cannot be determined without seeing the class code.

64. What will be the result of compiling and running the following program?

class Pra1 {
    public static void main(String[] args) {
        byte[][] ba={{1,2,3,4},{1,2,3}};
        System.out.println(ba[1].length+" "+ba.length);
    }
}

Answer: a) 3 2

66. Find the output / error of below java code.

class Pra1 {
    public static void main(String[] args) {
        int x=32;
        int y=2;
        if((x<y) && (++x<32))
            System.out.println(x++);
        System.out.println(x);
    }
}

Answer: b) 33