Java MCQ: Ultimate Trivia Quiz!

Reviewed by Editorial Team
The ProProfs editorial team is comprised of experienced subject matter experts. They've collectively created over 10,000 quizzes and lessons, serving over 100 million users. Our team includes in-house content moderators and subject matter experts, as well as a global network of rigorously trained contributors. All adhere to our comprehensive editorial guidelines, ensuring the delivery of high-quality content.
Learn about Our Editorial Process
| By Nagabhishek
N
Nagabhishek
Community Contributor
Quizzes Created: 1 | Total Attempts: 662
| Attempts: 662 | Questions: 29
Please wait...
Question 1 / 29
0 %
0/100
Score 0/100
1. What is the return type of a method that does not return any value?

Explanation

The return type of a method that does not return any value is "void". In programming, "void" is used to indicate that a method does not return any value. This means that when the method is called, it performs certain actions or operations but does not produce a result that needs to be returned or used by the caller.

Submit
Please wait...
About This Quiz
Java MCQ: Ultimate Trivia Quiz! - Quiz

Dive into the Java MCQ: Ultimate Trivia Quiz! Test your knowledge on Java fundamentals, including bytecode files, constructors, and JVM's role. Perfect for learners aiming to enhance their... see moreJava programming skills and understanding of Java's core components. see less

2. What is the extension of java code files?

Explanation

The extension of java code files is .java. This extension is used to identify files that contain Java programming code.

Submit
3. Which of these operators is used to allocate memory for an object?

Explanation

The operator "new" is used to allocate memory for an object. It is a keyword in programming languages like C++ and Java that dynamically allocates memory for an object at runtime. This allows the object to be created and initialized on the heap, rather than the stack, providing flexibility in memory management. The "new" operator returns a pointer to the newly allocated memory, which can then be used to access and manipulate the object.

Submit
4. What is true about constructors?

Explanation

Constructors are special methods in a class that are used to initialize objects. They do not have a return type, as their purpose is to create and initialize an object of the class. Therefore, the statement "It can contain a return type" is incorrect. However, constructors can take any number of parameters, allowing for flexibility in initializing objects. Additionally, constructors can have non-access modifiers, such as public or private, to control their accessibility. Finally, constructors can throw exceptions if necessary, so the statement "Constructor cannot throw an exception" is incorrect.

Submit
5. Which of these keywords must be used to inherit a class?

Explanation

The keyword "extends" must be used to inherit a class. In object-oriented programming, inheritance allows a class to inherit the properties and methods of another class. By using the "extends" keyword, a class can inherit from a superclass and access its members. This keyword establishes a parent-child relationship between classes, where the child class inherits the characteristics of the parent class.

Submit
6. What will be the output of the following Java program?  class box     {         int width;         int height;         int length;    }      class mainclass      {         public static void main(String args[])          {                     box obj = new box();             System.out.println(obj);         }      }

Explanation

The output of the program will be "classname@hashcode in hexadecimal form". This is because when we print an object in Java using the System.out.println() method, it calls the toString() method of the object. If the object does not have a custom implementation of the toString() method, it will use the default implementation provided by the Object class, which returns the classname followed by the hashcode of the object in hexadecimal form. In this case, since the box class does not have a custom toString() method, the default implementation is used.

Submit
7. How many methods does the Object class contain?

Explanation

The Object class in Java contains 11 methods. This class is the root of all classes in Java and provides basic functionalities that are inherited by all other classes. Some of the commonly used methods in the Object class include equals(), hashCode(), toString(), getClass(), and wait(). These methods are essential for object comparison, memory management, and obtaining information about an object's class and state. Overall, the Object class plays a crucial role in the Java programming language.

Submit
8. What is the extension of byte code files?

Explanation

The extension of byte code files is .class. This is because when Java source code is compiled, it is converted into byte code, which is a low-level representation of the code that can be executed by the Java Virtual Machine (JVM). The byte code is then stored in .class files, which can be executed on any platform that has a JVM installed.

Submit
9. How many primitive data types does java contain?

Explanation

Java contains 8 primitive data types, which are byte, short, int, long, float, double, char, and boolean. These data types are used to store simple values like numbers, characters, and true/false values.

Submit
10. What is the extension of compiled java classes?

Explanation

The extension of compiled Java classes is .class. This is because when Java code is compiled, it is converted into bytecode, which is then stored in .class files. These files can be executed by the Java Virtual Machine (JVM) to run the Java program. The .class extension is a standard convention in Java for compiled classes.

Submit
11.  Which of these can be overloaded?

Explanation

All of the mentioned options can be overloaded. Overloading is the process of defining multiple methods or constructors with the same name but different parameters. This allows for different variations of the method or constructor to be used depending on the arguments provided. Therefore, both methods and constructors can be overloaded to provide flexibility and versatility in programming.

Submit
12. What would be the behavior if the constructor has a return type?

Explanation

If the constructor has a return type, it would result in a compilation error. This is because constructors are special methods used to initialize objects and they do not have a return type. A constructor is automatically called when an object is created, and its purpose is to initialize the object's state. Therefore, having a return type for a constructor goes against the fundamental concept of constructors and would lead to a compilation error.

Submit
13. Which component is responsible for converting bytecode into machine-specific code?

Explanation

The JVM, or Java Virtual Machine, is responsible for converting bytecode into machine-specific code. Bytecode is the intermediate representation of Java programs that is generated by the Java compiler. The JVM then takes this bytecode and translates it into machine code that can be executed by the underlying hardware. This process is known as just-in-time (JIT) compilation, where the JVM dynamically compiles bytecode into machine code at runtime for improved performance. Therefore, the correct answer is JVM.

Submit
14. What will be the output of the following Java program? class A      {         int i;         public void display()          {             System.out.println(i);         }         }         class B extends A     {         int j;         public void display()          {             System.out.println(j);         }      }         class Dynamic_dispatch     {         public static void main(String args[])         {             B obj2 = new B();             obj2.i = 1;             obj2.j = 2;             A r;             r = obj2;             r.display();              }    }

Explanation

The output of the program will be 2. This is because the object "obj2" is of class B, which is a subclass of class A. When the display() method is called on the object "r", which is of type A but refers to an object of type B, the display() method of class B is executed. In class B, the variable "j" is printed, which has a value of 2.

Submit
15. What will be the output of the following Java program? class box      {         int width;         int height;         int length;         int volume;         void volume(int height, int length, int width)          {              volume = width*height*length;         }      }         class Prameterized_method     {         public static void main(String args[])         {             box obj = new box();             obj.height = 1;             obj.length = 5;             obj.width = 5;             obj.volume(3,2,1);             System.out.println(obj.volume);                 }       }  

Explanation

The program creates a class called "box" with instance variables width, height, length, and volume. It also has a method called "volume" which calculates the volume of the box using the provided height, length, and width values.

In the main method, an object of the box class is created and its height, length, and width are set to 1, 5, and 5 respectively. Then, the volume method is called with arguments 3, 2, and 1.

Since the volume method calculates the volume using the provided arguments, the volume of the box is 3 * 2 * 1 = 6. Therefore, the output of the program will be 6.

Submit
16. What are the components of a platform?

Explanation

The components of a platform typically include an operating system (OS) and a processor. The operating system provides the software framework for running applications and managing resources, while the processor handles the actual execution of instructions and data processing. The combination of these two components forms the foundation of a platform, allowing for the efficient and effective operation of various software applications and services.

Submit
17. In order to restrict a variable of a class from inheriting to subclass, how variable should be declared?

Explanation

In order to restrict a variable of a class from inheriting to subclass, the variable should be declared as private. Private variables can only be accessed within the class they are declared in, and cannot be accessed by any subclasses or other classes. This ensures that the variable remains hidden and inaccessible to any other classes, thereby restricting its inheritance.

Submit
18. Which of these keywords can be used to prevent Method overriding?

Explanation

The keyword "final" can be used to prevent method overriding. When a method is declared as final, it means that it cannot be overridden by any subclass. This is useful when you want to ensure that a particular method implementation remains unchanged in all subclasses, providing consistency and preventing any unintended modifications.

Submit
19. What will be the output of the following Java program? class A      {         int i;         void display()          {             System.out.println(i);         }     }         class B extends A      {         int j;         void display()          {             System.out.println(j);         }     }         class inheritance_demo      {         public static void main(String args[])         {             B obj = new B();             obj.i=1;             obj.j=2;                obj.display();              }    }

Explanation

The Java program defines two classes, A and B, where B is a subclass of A. Both classes have a method called display(). In the main method, an object of class B is created and its variables i and j are assigned values 1 and 2 respectively. Finally, the display() method of the object is called. Since the object is of class B, the display() method of class B is invoked, which prints the value of j, which is 2.

Submit
20. What will be the output of the following Java code? class overload      {         int x;      int y;         void add(int a)         {             x =  a + 1;         }         void add(int a , int b)         {             x =  a + 2;         }             }         class Overload_methods      {         public static void main(String args[])         {             overload obj = new overload();                int a = 0;             obj.add(6, 7);             System.out.println(obj.x);              }     }

Explanation

The code defines a class called "overload" with two methods named "add". The first method takes an integer parameter "a" and assigns the value of "a+1" to the variable "x". The second method takes two integer parameters "a" and "b" and assigns the value of "a+2" to the variable "x".

In the main method of the "Overload_methods" class, an object of the "overload" class is created. The "add" method of the object is called with the arguments 6 and 7. Then, the value of the variable "x" of the object is printed, which is 8.

Therefore, the output of the code will be 8.

Submit
21. All classes in Java are inherited from which class?

Explanation

All classes in Java are inherited from the class "java.lang.Object". This class is the root class of all classes in Java and provides a set of methods that are common to all objects. Therefore, any class that we create in Java automatically inherits the methods and properties of the Object class.

Submit
22. JVM can understand which type of file?

Explanation

JVM can understand .class files. JVM stands for Java Virtual Machine, which is responsible for executing Java programs. When a Java program is compiled, it is converted into bytecode and saved in a .class file. The JVM can read and understand this bytecode, allowing it to execute the Java program. Therefore, the correct answer is .class.

Submit
23. What will be the output of the following Java code? class test      {         int a;         int b;         void meth(int i , int j)          {             i *= 2;             j /= 2;         }               }         class Output      {         public static void main(String args[])         {             test obj = new test();         int a = 10;             int b = 20;                          obj.meth(a , b);             System.out.println(a + " " + b);                 }      }

Explanation

The output of the code will be "10 20". This is because the method "meth" in the "test" class does not modify the values of the variables "a" and "b" passed to it. Therefore, when the values of "a" and "b" are printed in the main method, they will still be the original values of 10 and 20 respectively.

Submit
24. Which component is used to compile, debug, and execute the java program?

Explanation

JDK, which stands for Java Development Kit, is used to compile, debug, and execute Java programs. It includes the Java compiler, debugger, and other necessary tools for developing Java applications. JVM (Java Virtual Machine) is responsible for executing Java bytecode, while JRE (Java Runtime Environment) provides the runtime environment for running Java applications. JIT (Just-In-Time) is a component of the JVM that dynamically compiles bytecode into machine code for improved performance. However, for the complete development process, including compilation and debugging, JDK is the appropriate choice.

Submit
25. What is it called where a child object gets killed if the parent object is killed?

Explanation

Composition is a relationship between two objects where the child object is a part of the parent object and cannot exist independently. In composition, if the parent object is destroyed or killed, the child object is also destroyed or killed. This is because the child object is tightly coupled with the parent object and is completely dependent on it for its existence. Therefore, composition is the correct answer to the question.

Submit
26. Predict the output of following Java program? class Test {    int i;  }   class Main {     public static void main(String args[]) {        Test t;        System.out.println(t.i);   }   

Explanation

The program will give a compiler error because the variable "t" of type Test is not initialized before trying to access its member variable "i". Therefore, it is not possible to predict the output of this program.

Submit
27. What will be the output of the following Java program?  class box      {         int width;         int height;         int length;     }      class mainclass      {         public static void main(String args[])          {                     box obj1 = new box();             box obj2 = new box();             obj1.height = 1;             obj1.length = 2;             obj1.width = 1;             obj2 = obj1;             System.out.println(obj2.height);         }      }

Explanation

The program creates two objects of the "box" class, obj1 and obj2. The values of obj1's height, length, and width are set to 1, 2, and 1 respectively. Then, obj2 is assigned the value of obj1. Since obj1 and obj2 refer to the same object in memory, obj2.height will have the same value as obj1.height, which is 1. Therefore, the output of the program will be 1.

Submit
28. What is the data type of any class you write?

Explanation

The data type of any class that you write is non primitive. This is because classes are considered reference types and are not built-in or pre-defined in the programming language. Unlike primitive data types (such as int, float, boolean), which have a fixed size and are directly stored in memory, objects of a class are created dynamically and stored in heap memory. Therefore, the data type of a class is non primitive.

Submit
29. What will be the output of the following Java program? class Output      {         static void main(String args[])          {                  int x , y = 1;              x = 10;              if(x != 10 && x / 0 == 0)                  System.out.println(y);              else                  System.out.println(++y);         }      }

Explanation

The output of the program will be 2. In the if statement, the condition x != 10 will evaluate to false since x is assigned the value 10. Therefore, the second part of the condition x / 0 == 0 will not be checked, and the code inside the if statement will not be executed. Instead, the code inside the else statement will be executed, which increments the value of y by 1 and prints it, resulting in the output of 2.

Submit
View My Results

Quiz Review Timeline (Updated): Mar 22, 2023 +

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

  • Current Version
  • Mar 22, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • Dec 06, 2019
    Quiz Created by
    Nagabhishek
Cancel
  • All
    All (29)
  • Unanswered
    Unanswered ()
  • Answered
    Answered ()
What is the return type of a method that does not return any value?
What is the extension of java code files?
Which of these operators is used to allocate memory for an object?
What is true about constructors?
Which of these keywords must be used to inherit a class?
What will be the output of the following Java program?...
How many methods does the Object class contain?
What is the extension of byte code files?
How many primitive data types does java contain?
What is the extension of compiled java classes?
 Which of these can be overloaded?
What would be the behavior if the constructor has a return type?
Which component is responsible for converting bytecode into...
What will be the output of the following Java program?...
What will be the output of the following Java program?...
What are the components of a platform?
In order to restrict a variable of a class from inheriting to...
Which of these keywords can be used to prevent Method overriding?
What will be the output of the following Java program?...
What will be the output of the following Java code?...
All classes in Java are inherited from which class?
JVM can understand which type of file?
What will be the output of the following Java code?...
Which component is used to compile, debug, and execute the java...
What is it called where a child object gets killed if the parent...
Predict the output of following Java program?...
What will be the output of the following Java program?...
What is the data type of any class you write?
What will be the output of the following Java program?...
Alert!

Advertisement