Java Mcq’s

1. What is the order of variables in Enum?
a) Ascending order
b) Descending order
c) Random order
d) Depends on the order() method

Answer: a
Explanation: The compareTo() method is implemented to order the variable in ascending order.

2. Can we create an instance of Enum outside of Enum itself?
a) True
b) False

Answer: b
Explanation: Enum does not have a public constructor.

3.Which method returns the elements of Enum class?
a) getEnums()
b) getEnumConstants()
c) getEnumList()
d) getEnum()

Answer: b
Explanation: getEnumConstants() returns the elements of this enum class or null if this Class object does not represent an enum type.

4. If we try to add Enum constants to a TreeSet, what sorting order will it use?
a) Sorted in the order of declaration of Enums
b) Sorted in alphabetical order of Enums
c) Sorted based on order() method
d) Sorted in descending order of names of Enums

Answer: a
Explanation: Tree Set will sort the values in the order in which Enum constants are declared.

5. What will be the output of the following Java code snippet?

  1. class A
  2. {
  3.  
  4. }
  5.  
  6. enum Enums extends A
  7. {
  8. ABC, BCD, CDE, DEF;
  9. }

a) Runtime Error
b) Compilation Error
c) It runs successfully
d) EnumNotDefined Exception

Answer: b
Explanation: Enum types cannot extend class.

6. What will be the output of the following Java code snippet?

  1. enum Levels
  2. {
  3. private TOP,
  4.  
  5. public MEDIUM,
  6.  
  7. protected BOTTOM;
  8. }

a) Runtime Error
b) EnumNotDefined Exception
c) It runs successfully
d) Compilation Error

Answer: d
Explanation: Enum cannot have any modifiers. They are public, static and final by default.

7. What will be the output of the following Java code snippet?

  1. enum Enums
  2. {
  3. A, B, C;
  4.  
  5. private Enums()
  6. {
  7. System.out.println(10);
  8. }
  9. }
  10.  
  11. public class MainClass
  12. {
  13. public static void main(String[] args)
  14. {
  15. Enum en = Enums.B;
  16. }
  17. }

a)10 10 10

b) Compilation Error
c)10 10

d) Runtime Exception

Answer: a
Explanation: The constructor of Enums is called which prints 10.

8.What will be the output of the following Java code?

  1. enum Season
  2. {
  3. WINTER, SPRING, SUMMER, FALL
  4. };
  5. System.out.println(Season.WINTER.ordinal());

a) 0
b) 1
c) 2
d) 3

Answer: a
Explanation: ordinal() method provides number to the variables defined in Enum.

9. Which class does all the Enums extend?
a) Object
b) Enums
c) Enum
d) EnumClass

Answer: c
Explanation: All enums implicitly extend java.lang.Enum. Since Java does not support multiple inheritance, an enum cannot extend anything else.

10. Are enums are type-safe?
a) True
b) False

Answer: a
Explanation: Enums are type-safe as they have own name-space.

11. Which of the following is not OOPS concept in Java?
a) Inheritance
b) Encapsulation
c) Polymorphism
d) Compilation

Answer: d
Explanation: There are 4 OOPS concepts in Java. Inheritance, Encapsulation, Polymorphism and Abstraction.

12. Which of the following is a type of polymorphism in Java?
a) Compile time polymorphism
b) Execution time polymorphism
c) Multiple polymorphism
d) Multilevel polymorphism

Answer: a
Explanation: There are two types of polymorphism in Java. Compile time polymorphism (overloading) and runtime polymorphism (overriding).

13. When does method overloading is determined?
a) At run time
b) At compile time
c) At coding time
d) At execution time

Answer: b
Explanation: Overloading is determined at compile time. Hence, it is also known as compile time polymorphism.

14. When Overloading does not occur?
a) More than one method with same name but different method signature and different number or type of parameters
b) More than one method with same name, same signature but different number of signature
c) More than one method with same name, same signature, same number of parameters but different type
d) More than one method with same name, same number of parameters and type but different signature

Answer: d
Explanation: Overloading occurs when more than one method with same name but different constructor and also when same signature but different number of parameters and/or parameter type.

15. Which concept of Java is a way of converting real world objects in terms of class?
a) Polymorphism
b) Encapsulation
c) Abstraction
d) Inheritance

Answer: c
Explanation: Abstraction is the concept of defining real world objects in terms of classes or interfaces.

16. Which concept of Java is achieved by combining methods and attribute into a class?
a) Encapsulation
b) Inheritance
c) Polymorphism
d) Abstraction

Answer: a
Explanation: Encapsulation is implemented by combining methods and attribute into a class. The class acts like a container of encapsulating properties.

17. What is it called if an object has its own lifecycle and there is no owner?
a) Aggregation
b) Composition
c) Encapsulation
d) Association

Answer: d
Explanation: It is a relationship where all objects have their own lifecycle and there is no owner. This occurs where many to many relationships are available, instead of one to one or one to many.

18. What is it called where child object gets killed if parent object is killed?
a) Aggregation
b) Composition
c) Encapsulation
d) Association

Answer: b
Explanation: Composition occurs when child object gets killed if parent object gets killed. Aggregation is also known as strong Aggregation.

19. What is it called where object has its own lifecycle and child object cannot belong to another parent object?
a) Aggregation
b) Composition
c) Encapsulation
d) Association

Answer: a
Explanation: Aggregation occurs when objects have their own life cycle and child object can associate with only one parent object.

20. Method overriding is combination of inheritance and polymorphism?
a) True
b) false

Answer: a
Explanation: In order for method overriding, method with same signature in both superclass and subclass is required with same signature. That satisfies both concepts inheritance and polymorphism.

31. Which component is used to compile, debug and execute java program?
a) JVM
b) JDK
c) JIT
d) JRE

Answer: b
Explanation: JDK is a core component of Java Environment and provides all the tools, executables and binaries required to compile, debug and execute a Java Program.

32. Which component is responsible for converting bytecode into machine specific code?
a) JVM
b) JDK
c) JIT
d) JRE

Answer: a
Explanation: JVM is responsible to converting bytecode to the machine specific code. JVM is also platform dependent and provides core java functions like garbage collection, memory management, security etc.

33. Which component is responsible to run java program?
a) JVM
b) JDK
c) JIT
d) JRE

Answer: d
Explanation: JRE is the implementation of JVM, it provides platform to execute java programs.

34. Which component is responsible to optimize bytecode to machine code?
a) JVM
b) JDK
c) JIT
d) JRE

Answer: c
Explanation: JIT optimizes bytecode to machine specific language code by compiling similar bytecodes at the same time. This reduces overall time taken for compilation of bytecode to machine specific language.

35. Which statement is true about java?
a) Platform independent programming language
b) Platform dependent programming language
c) Code dependent programming language
d) Sequence dependent programming language

Answer: a
Explanation: Java is called ‘Platform Independent Language’ as it primarily works on the principle of ‘compile once, run everywhere’.

36. Which of the below is invalid identifier with the main method?
a) public
b) static
c) private
d) final

Answer: c
Explanation: main method cannot be private as it is invoked by external method. Other identifier are valid with main method.

37. What is the extension of java code files?
a) .class
b) .java
c) .txt
d) .js

Answer: b
Explanation: Java files have .java extension.

38. What is the extension of compiled java classes?
a) .class
b) .java
c) .txt
d) .js

Answer: a
Explanation: The compiled java files have .class extension.

39. How can we identify whether a compilation unit is class or interface from a .class file?
a) Java source file header
b) Extension of compilation unit
c) We cannot differentiate between class and interface
d) The class or interface name should be postfixed with unit type

Answer: a
Explanation: The Java source file contains a header that declares the type of class or interface, its visibility with respect to other classes, its name and any superclass it may extend, or interface it implements.

40. What is use of interpreter?
a) They convert bytecode to machine language code
b) They read high level code and execute them
c) They are intermediated between JIT and JVM
d) It is a synonym for JIT

Answer: b
Explanation: Interpreters read high level language (interprets it) and execute the program. Interpreters are normally not passing through byte-code and jit compilation.

error: Content is protected !!