Core Java MCQs

Core Java MCQs

Answer these 100+ Core Java MCQs and assess your grip on the subject of Core Java.
Scroll below and get started!

1: Which one of the following is NOT a reserved word in Java?

A.   public

B.   private

C.   virtual

D.   final

2: What will be the output of program? class Ab { public static void main(String[] ar) {System.out.print("hi"); } static { System.out.print("how"); } }

A.   runtime error

B.   hihow

C.   howhi

D.   compilation error: At Static

3: The Core Java platform provides many benefits to developers, including:

A.   A consistent programming interface across multiple hardware platforms

B.   Superior speed and performance compared to native code

C.   A purely functional programming language with a minimalist design philosophy

D.   Direct compilation to native code on most platforms

4: What is the correct way to create an instance of a class?

A.   ClassName varName = new ClassName(new ClassName);

B.   ClassName varName = new ClassName(arguments);

C.   varName ClassName = new varName(arguments);

D.   ClassName varName => new ClassName();

5: True of False? The strictfp keyword ensures that you get the same result on every platform if you perform operations in the floating point variable.

A.   True

B.   False

6: Given following code, what will be the output: import java.lang.reflect.Method; class Bar { private int bar(int a) { return a * a; } } public class Foo { public static void main(String[] args) { try { Bar bar = new Bar(); Method method = bar.getClass().getDeclaredMethod("bar", int.class); method.setAccessible(true); System.out.println(method.invoke(bar, new Integer(5))); } catch (Exception e) { e.printStackTrace(); System.out.println("Error"); } } }

A.   25

B.   Null

C.   IllegalAccessException will be thrown

D.   CompileError

E.   5

7: Interfaces are useful for...

A.   making an abstract class concrete

B.   reducing heap size

C.   creating a design contract that encapsulates implementation

D.   implementing an abstract factory pattern

8: The Object.wait() method:

A.   Resumes from waiting if notifyAll() is invoked for the object

B.   Resumes from waiting if notify() is invoked for the object

C.   Causes the current thread to wait

D.   Resumes from waiting if a specified amount of time has elapsed

9: Which will legally declare, construct, and initialize an array?

A.   int myList [] [] = {4,9,7,0};

B.   int [] myList = {"1", "2", "3"};

C.   int [] myList = (5, 8, 2);

D.   int myList [] = {4, 3, 7};

10: A java class which extends another class is usually described with the word:

A.   subclass

B.   dynamic

C.   abstract

D.   overloaded

11: Finally is used to....

A.   ensure a block of code is executed when the JVM shuts down.

B.   ensure a block of code is executed only when try/catch completes without an exception

C.   ensure a block of code is executed only when try/catch completes with an exception

D.   ensure a block of code is always executed after a try/catch

12: If a method or variable is marked as having the "private" access level, then it can only be accessed from:

A.   Inside the same class

B.   Inside the same class, or any of its superclasses

C.   Inside the same class, or a subclass

D.   Inside the same class or its parent class

13: What is an example of proper capitalization for a class name?

A.   camELCase

B.   CAMELcase

C.   CamelCase

D.   camelcase

14: The "javac" command line tool is used to:

A.   Convert java bytecode files into native executables

B.   Compress collections of java class files into .jar archives

C.   Compile java source files into bytecode class files

D.   Generate C headers and stubs for native methods

15: Java handles memory allocation and reuse using a process called:

A.   Buddy Blocks

B.   Garbage Collection

C.   Virtual Memory

D.   Manual Memory Management

16: Which additional keyword may be used with try-catch blocks?

A.   finish

B.   finalize

C.   finally

D.   final

17: The most reliable way to compare two Strings for equality is by:

A.   Using the .equals() or .compareTo() method of one object on the other

B.   Using the == operator on the .value() of each object

C.   Using the &= operator on the objects

D.   Using the == operator on the objects

18: The part of a "try" block that is always executed is:

A.   "if"

B.   "import"

C.   "enum"

D.   "finally"

19: To define a child class from the Parent class following is used:

A.   class Child extends Parent

B.   class Child : Parent

C.   class Child extends Public Parent

D.   class Child :: Parent

20: What is the correct syntax for importing java.util.Scanner?

A.   import. java.util.Scanner;

B.   import.java.util.scanner;

C.   import.java.util.scanner.

D.   import java.util.Scanner;

21: If we want a class not to be overridden,the class must be done as

A.   Class should be abstract

B.   Class should public

C.   Class should be final

D.   Class should be static

22: To document an API, which tool do you use?

A.   javaApi

B.   documentcreate

C.   javadoc

D.   apicreate

23: Which of these are advantages of encapsulation in Java?

A.   Encapsulation reduces coupling of modules and increase cohesion inside a module

B.   Encapsulation in Java makes unit testing easy

C.   All of these

D.   Encapsulated code is easy to change with new requirements

24: What is the most efficient way to concatenate a large number of strings in Java?

A.   The StringBuffer object.

B.   The + operator.

25: Which of the following is a valid constructor signature?

A.   public void className()

B.   public static className()

C.   static className()

D.   public className()

26: The Thread.sleep() method:

A.   Suspends execution in synchronized methods only

B.   Causes the hosted virtual machine to suspend all forms of execution

C.   Causes all threads to suspend execution

D.   Causes the current thread to suspend execution

27: How can you stop your class from being inherited by another class?

A.   Declare the class default constructor as private.

B.   Declare the class as final.

C.   Declare the class as abstract.

D.   It’s not possible.

28: public class SomeClass { public static void main(String[] args) { System.out.println((String) null); } } Does this code compile without errors?

A.   Yes

B.   No

29: When you create a thread with the “new” operator – which one of the following statements is true about its state

A.   it will be “runnable” when start() method is called

B.   it is in “runnable” state

C.   it is blocked until another thread calls notify()

D.   it starts running immediately

30: What method should you always override when you have overridden the equals() method?

A.   hashCode()

B.   toString()

C.   wait()

D.   clone()

31: Which of these is true?

A.   An interface implements another interface and class

B.   A class implements and extends a class

C.   An interrface extends a class but implements another interface

D.   A class implements an interface but extends a class

32: Can an abstract class be a final class?

A.   No

B.   Yes

33: Keyword used to access members or methods of superclass?

A.   this

B.   Super

C.   extends

D.   native

34: What is auto boxing?

A.   JVM conversion of int to float values

B.   Automatic insertion of brackets by an IDE

C.   It doesn’t occur in Java, only in dynamically typed JVM languages like Groovy

D.   JVM conversion between primitive types and reference types

35: Reflection mechanism allows to:

A.   Delegate configuration to XML file

B.   Modify and inspect properties and methods of objects, even if they are protected or private

36: package test; class Test { } ---------------- package test; class SubTest extends Test{ } Does this code compile without errors if the classes are in separate files?

A.   No

B.   Yes

37: What will be the output of the program? public class Foo { public static void main(String[] args) { try { return; } finally { System.out.println( "Finally" ); } } }

A.   Finally

B.   Compilation fails.

C.   The code runs with no output.

D.   An exception is thrown at runtime.

38: How can we use the class or jar files kept on the network path, within our projects?

A.   mentioning the Class /Jar file names during compilation only

B.   by directly copying and including in the same folder as of the project

C.   No the network files can not be used directly

D.   Including the path and class /jar file name in the Classpath

E.   mentioning the file names in the Path

39: What will below method return? public foo(){ int x = 5; return x; }

A.   Runtime error

B.   5

C.   null

D.   Compile error

40: What is the output of the below code ? int a = 0; int b = 0; if (a++ == 1 || b++ == 1) {} System.out.println(a + " " + b);

A.   0 1

B.   0 0

C.   1 0

D.   1 1

41: How should you create a new class that maps keys to values, using the Java Collections Framework?

A.   Implement the Queue, List, and Array interfaces

B.   Implement the Map interface, possibly by extending the AbstractMap class

C.   Implement both the Iterator and Array interfaces

D.   Extend the AbstractCollection class, thereby implementing the AbstractCollection interface

42: What is the benefit of ConcurrentHashMap<K,V>?

A.   Allows null to be used a key or value

B.   It maintains a list through all entries to retrieve data in the order it was inserted.

C.   All operations are thread-safe and retrieval operations do not entail locking

D.   Supports locking the entire table in a way that prevents all access

43: The "java" command line tool is used to:

A.   Compress collections of java class files into .jar archives

B.   Load and execute java .class files

C.   Disassemble .class files back into readable source code

D.   Compile java source files into bytecode class files

44: JDBC addresses the issue of transactions.

A.   True

B.   False

45: Is it a good pratcice to catch "Throwable"?

A.   no

B.   yes

46: The "static" keyword marks something as:

A.   Not being mutable after initialization

B.   A constant variable whose value cannot be changed

C.   No longer able to be subclassed or overloaded

D.   Belonging to a class, rather than a specific instance

47: What are all the different types of access modifiers in Java

A.   private, public

B.   private, protected, default, public

C.   private, protected, public

D.   protected, default, public

E.   private, default, public

48: On which Java major release were Lambdas introduced?

A.   Java 6

B.   Java 5

C.   Java 7

D.   Java 8

49: When creating a user defined class for storing objects in a HashMap, which method(s) should be overridden?

A.   The equal() method

B.   The hashCode() method

C.   The constructor method

D.   Both the equals() and hashCode() methods

E.   (You don't need override any methods)

50: The instanceof operator can be used to determine if an object is:

A.   An instance of a class that implements a given interface

B.   (All of these)

C.   An instance of a class

D.   An instance of a subclass of a class