Java MCQ Quiz - Objective Question with Answer for Java - Download Free PDF

Last updated on Feb 14, 2023

Latest Java MCQ Objective Questions

Java Question 1:

Match List I with List II:

List I List II
(A) Localization (I) Encapsulation
(B) Packaging or binding of a collection of items (II) Abstraction
(C) Mechanism that enables designer to focus on essential details of a program component. (III) Characteristic of software that indicates the manner in which information is concentrated in program
(D) Information hiding (IV) Suppressing the operational details of a program component


Choose the correct answer from the options given below:

  1. ​(A) - (I), (B) - (II), (C) - (III), (D) - (IV)
  2. (A) - (II), (B) - (I), (C) - (III), (D) - (IV)
  3. (A) - (III), (B) - (I), (C) - (II), (D) - (IV)
  4. (A) - (III), (B) - (I), (C) - (IV), (D) - (II)

Answer (Detailed Solution Below)

Option 3 : (A) - (III), (B) - (I), (C) - (II), (D) - (IV)

Java Question 1 Detailed Solution

Concept:

  • Localization is a characteristic of software that indicates the manner in which information is concentrated within a program. For example, conventional methods for functional decomposition localize information around functions, which are typically implemented as procedural modules.
  • Abstraction is a mechanism that enables the designer to focus on the essential details of a program component with little concern for lower-level details.
  •  Encapsulation is to make sure that "sensitive" data is hidden from users and wrapping code and data together into a single unit.
  •  Information hiding is the ability to prevent certain aspects of a class or software component from being accessible to its clients and Suppressing the operational details of a program component.

The Correct Answer is option 3.

Java Question 2:

Which of the following statements is/are true regarding JAVA?

A. Constants that cannot be changed are declared using the 'Static' keyword.

B. A class can only inherit one class that can implement multiple interfaces.

  1. A is true
  2. B is true
  3. Both (A) and (B) are true
  4. Neither (A) nor (B) is true

Answer (Detailed Solution Below)

Option 2 : B is true

Java Question 2 Detailed Solution

Correct Answer: Option 2

Explanation:

  • Constants that cannot be changed are declared using the final keyword. The static keyword is used for functions and variables to specify that they will be instantiated exactly once for any number of object instances and the same value will be used across all objects.
  • Java does not support multiple inheritances directly. When a class inherits two different classes, it is known as multiple inheritances. Java does not allow for a class to inherit more than one class. However, two classes can be inherited if one of them is an interface. An interface is a schematic representation of a class containing variables and abstract methods. So, a class can inherit one class, that can implement multiple interfaces.

Therefore, statement A is incorrect and statement B is correct

Java Question 3:

Which of the following methods will create a string in Java?

I: String S = "Hello Java”;

II: String S2 = new String (“Hello Java");

  1. Only I
  2. Only II
  3. Both I and II
  4. Neither I nor II

Answer (Detailed Solution Below)

Option 3 : Both I and II

Java Question 3 Detailed Solution

The correct answer is option 3.

Concept:

A string is an object in Java that indicates a collection of characters or char values. A Java string object is created using the java.lang.String class.

A String object can be created in one of two ways:

By string literal:

Java String literal is created by using double-quotes.

Example:

 String s=“Welcome”;  

By new keyword:

Java String is created by using the keyword “new”.

Example:

String s=new String(“Welcome”);  

Hence the correct answer is Both I and II.

Java Question 4:

In Structured Design, communication between different modules means ____________.

  1. Inheritance
  2. Encapsulation
  3. Cohesion
  4. Coupling

Answer (Detailed Solution Below)

Option 4 : Coupling

Java Question 4 Detailed Solution

The correct answer is option 4.

Concept:

Structured design:

Structured design is the conception of a problem into multiple well-organized solution pieces. It is primarily concerned with the design of the solution. Cohesion and coupling are two criteria that structured design always follows for communication across numerous components.

Cohesion:

  • Cohesion refers to the Grouping of all functionally related elements. 
  • The degree to which the pieces inside a module belong together is referred to as cohesiveness.

Coupling:

  • Coupling refers to the communication between different modules.
  • Coupling is the degree of dependency between software modules. It is a measure of how closely coupled two procedures or modules are; it is the strength of the links between modules.

Hence the correct answer is Coupling.

Java Question 5:

____________________ is an API to develop Graphical User Interface (GUI) or Windows-based applications in Java.

  1. Java Abstract Window Toolkit
  2. Java Regex
  3. Java Applet
  4. Java I/O

Answer (Detailed Solution Below)

Option 1 : Java Abstract Window Toolkit

Java Question 5 Detailed Solution

The correct answer is option 1.

Concept:

Java Abstract Window Toolkit:

The Java AWT (Abstract Window Toolkit) API is used to create a graphical user interface (GUI) or windows-based Java applications. It is a set of application program interfaces (APIs) that Java programmers use to construct GUI components like buttons, scroll bars, and windows. Sun Microsystems, the company that invented Java, created AWT as part of the Java Foundation Classes (JFC).

Hence the correct answer is Java Abstract Window Toolkit.

Additional Information

  •  A Java Applet is a sort of application that generates dynamic information by being embedded in a webpage. It is a client-side application that runs within the browser.
  • The Java Regex or Regular Expression is an API to define a pattern for searching or manipulating strings.
  • Java I/O (Input and Output) is used to process the input and produce the output.

Top Java MCQ Objective Questions

What is the use of 'javac' command?

  1. Execute a java program
  2. Debug a java program
  3. Interpret a java program
  4. Compile a java program

Answer (Detailed Solution Below)

Option 4 : Compile a java program

Java Question 6 Detailed Solution

Download Solution PDF

Concept

The javac command in Java compiles a program from a command prompt.

It reads a Java source program from a text file and creates a compiled Java class file.

Syntax

javac filename [options]

For example, to compile a program named Abc.java, use this command:

javac Abc.java

Consider the following Java code fragment. Which of the following statement is true?

Line No

Code Statement

1

public class While

2

{

3

      public void loop()

4

      {

5

         int x = 0;

6

         while (1)

7

          {

8

System.out.println(“x plus one is” + (x + 1));

9

           }

10

      }

11

}

  1. There is syntax error in line no. 1
  2. There are syntax errors in line nos. 1 & 6
  3. There is syntax error in line no. 8
  4. There is syntax error in line no. 6

Answer (Detailed Solution Below)

Option 4 : There is syntax error in line no. 6

Java Question 7 Detailed Solution

Download Solution PDF

The correct answer is “option 4”.

EXPLANATION:

Option 1: FALSE

While” is notkeyword, hence it is valid to use it as a class name.

Option 2: FALSE

Since Option 1 is false, so this option is by default false.

Option 3: FALSE

Any string operation can contain an equation type of expression.

So, this is not any kind of syntax error.

Option 4: TRUE

Java uses condition as Boolean expressions like:   

while(true) or while(false)

Hence, while(1) is the wrong syntax for java.

while(1) will give compiler time error as it treats as type mismatch to convert from integer to boolean value.

Hence, the correct answer is “option 4”.

Mistake Points 

Here Class name is While used which is not a keyword. while is a keyword in java but not While. So, we can use it as class name. 

Which of the following is/are the rules to declare variables in Java?

  1. Java keywords cannot be used as variable names
  2. All of the options
  3. Variable names are case-sensitive
  4. The first character must be a letter

Answer (Detailed Solution Below)

Option 2 : All of the options

Java Question 8 Detailed Solution

Download Solution PDF

The variable is the basic unit of storage in a Java program. A variable is defined by the combination of an identifier, a type, and an optional initializer.

Variable names cannot be keyword, it is case- sensitive and the first character must be a letter.

In Java, all variables must be declared before they can be used. The basic form of a variable declaration is:

type identifier [ = value ][, identifier [= value ] …];

Examples:

int a, b;

int a = 10, b = 20, c = 30;

int a, b = 20, c;

Confusion point

In Java, variable name can also start with underscore character “_”, or a dollar sign “$”.

Best Possible answer is chosen.

Which of the following methods will create a string in Java?

I: String S = "Hello Java”;

II: String S2 = new String (“Hello Java");

  1. Only I
  2. Only II
  3. Both I and II
  4. Neither I nor II

Answer (Detailed Solution Below)

Option 3 : Both I and II

Java Question 9 Detailed Solution

Download Solution PDF

The correct answer is option 3.

Concept:

A string is an object in Java that indicates a collection of characters or char values. A Java string object is created using the java.lang.String class.

A String object can be created in one of two ways:

By string literal:

Java String literal is created by using double-quotes.

Example:

 String s=“Welcome”;  

By new keyword:

Java String is created by using the keyword “new”.

Example:

String s=new String(“Welcome”);  

Hence the correct answer is Both I and II.

What is garbage collection in the context of Java?

  1. The operating system periodically deletes all of the Java files available on the system.  
  2. When all references to an object are gone, then the memory used by the object is automatically reclaimed. 
  3.  Any java package imported in a program and not being used, is automatically deleted. 
  4. The java virtual machine (JVM) checks the output of any java program and deleted anything that does not make sense at all. 

Answer (Detailed Solution Below)

Option 2 : When all references to an object are gone, then the memory used by the object is automatically reclaimed. 

Java Question 10 Detailed Solution

Download Solution PDF

Concept:

  • Java garbage collection is the process of releasing unused memory.
  • Sometimes some objects are no longer required by the program and when there is no reference to an object, then that object should be released.
  • This process is known as garbage collection.

Explanation:

  • Garbage collection process is done by JVM (java virtual machine). Unreachable and unusable objects are available for garbage collection.
  • With garbage collection, the programmer has no need to worry about dereferencing the object. It increases memory efficiency and less memory leakage. 
  • For this, make the object reference null so that it is deleted by garbage collection.

 

Example: obj obj1 = new obj();

obj1 = null;              // for garbage collection

A condition that is caused by run-time error in a computer program is known as: 

  1. Syntax error
  2. Fault
  3. Semantic error
  4. Exception

Answer (Detailed Solution Below)

Option 4 : Exception

Java Question 11 Detailed Solution

Download Solution PDF

Concept:

In computer programming, an exception is a special condition encountered during program execution or run time. Example: if the program tries to open a file that does not exist, divide by zero, etc.

Explanation:

When an error occurs within a method, the method creates an object known as an exception object which contains information about the error. This process is known as throwing an exception.

After this, the run time system finds something to handle it. Run time system searches the list of methods that can handle the exception. Search begins with the method in which the error occurred and proceeds through the list of methods in reverse order. Exception handler chosen is said to catch the exception.

There are two blocks to handle an exception: try and catch block.

Try block is used to enclose the code that throws an exception. It is used within the method. Catch block is used to handle the exception by declaring the type of exception with the parameter. Catch block must be used after the try block.

Syntax :

try

{

//code that throws exception

}

catch(exception_class_name){}

Additional information sent when an exception is thrown may be placed in ______

  1. The throw keyword
  2. The function that caused the error
  3. The catch block
  4. An object of the exception class

Answer (Detailed Solution Below)

Option 3 : The catch block

Java Question 12 Detailed Solution

Download Solution PDF

Concept:

An exception is an unexpected event which occurs during run time which disrupts the normal flow of execution. Example: division by zero.

There are two blocks in this: try and catch block.

Explanation:

try block:

It contains set of statements where an exception can occur. It is always followed by a catch block.

catch block:

In this block, exceptions are handled. Additional information sent when an exception is thrown is placed in this block. A single try block can have multiple catch blocks. Throw keyword is used to transfer control from try block to catch block.

syntax:

try

{

// statement that causes exception

}

catch

{

//statements that handle exception

}

The static keyword word is used in public static void main() declaration in Java__________.

  1. To enable the JVM to make call to the main(), as class has not been instantiated.
  2. To enable the JVM to make call to the main(), as class has not been inherited.
  3. To enable the JVM to make call to the main(), as class has not been loaded.
  4. To enable the JVM to make call to the main(), as class has not been finalized.

Answer (Detailed Solution Below)

Option 1 : To enable the JVM to make call to the main(), as class has not been instantiated.

Java Question 13 Detailed Solution

Download Solution PDF

Concept:

The static keyword word is used in the public static void main() declaration in Java to enable the JVM to make a call to the main(), as class has not been instantiated.

Reason:

main() method:

The main() method, in Java, is the entry point for the JVM(Java Virtual Machine) into the java program. JVM launches the java program by invoking the main() method o enable the JVM to make call to the main(), as class has not been instantiated.

Static is a keyword.

  • The role of adding static before any entity is to make that entity a class entity.
  • It means that adding static before methods and variables makes them class methods and class variables respectively, instead of instance methods and instance variables.
  • Hence, static methods and variables can be directly accessed with the help of Class, which means that there is no need to create objects in order to access static methods or variables.

What is the output of this program?

class char_increment {

public static void main (String args[ ])

{

char c1 = 'D';

char c2 = 84;

c2++;

c1++;

System.out.println(c1 + " " + c2);

}

}

  1. E U
  2. U E
  3. V E 
  4. U F 

Answer (Detailed Solution Below)

Option 1 : E U

Java Question 14 Detailed Solution

Download Solution PDF

Correct answer: option 1

Explanation:

  • ASCII value of D is 68 and E is 69
  • When c2++ is executed, the value of c2 becomes 85. This is the ASCII value for the character 'U'
  • When c1++ is executed, the value of c1 becomes the character 'E'
  • When c1 and c2 are printed with a space is between them, the output is   E U

 

Important Note:

  • When a char variable is assigned a number, the number is treated as an ASCII code and the corresponding character is stored in the variable.
  • When a char variable is incremented, the corresponding ASCII value of the character is incremented. This results in the character held by the variable being modified.

In Java, the Dynamic Array are known as:

  1. Vectors
  2. Cycle
  3. Remote
  4. Kubernotos

Answer (Detailed Solution Below)

Option 1 : Vectors

Java Question 15 Detailed Solution

Download Solution PDF

Concept: 

Dynamic arrays are those arrays that are allocated memory at the run time with the help of a heap. Thus Dynamic array can change its size during run time

A dynamic array expands as you add more elements. So you don't need to determine the size ahead of time.

Important Points 

Vector is like a dynamic array that can grow or shrink its size. Unlike an array, we can store n-number of elements in it as there is no size limit. It is a part of the Java Collection framework since Java 1.2. It is found in java.util package and implements the List interface, so we can use all the methods of the List interface here. It is similar to the ArrayList, but with two differences-

  • Vector is synchronized.
  • Java Vector contains many legacy methods that are not part of a collections framework.

Java Vector class Declaration

public class Vector  

extends Object  

implements List, Cloneable, Serializable  

Hence the correct answer is option 1