Java MCQ: Ultimate Trivia Quiz!

Approved & Edited by ProProfs Editorial Team
The editorial team at ProProfs Quizzes consists of a select group of subject experts, trivia writers, and quiz masters who have authored over 10,000 quizzes taken by more than 100 million users. This team includes our in-house seasoned quiz moderators and subject matter experts. Our editorial experts, spread across the world, are rigorously trained using our comprehensive guidelines to ensure that you receive the highest quality quizzes.
Learn about Our Editorial Process
| By Nagabhishek
N
Nagabhishek
Community Contributor
Quizzes Created: 1 | Total Attempts: 632
Questions: 29 | Attempts: 632

SettingsSettingsSettings
Java MCQ: Ultimate Trivia Quiz! - Quiz


Questions and Answers
  • 1. 

    What is the extension of byte code files?

    • A.

      .java

    • B.

      .class

    • C.

      .xml

    • D.

      .byte

    Correct Answer
    B. .class
    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.

    Rate this question:

  • 2. 

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

    • A.

      Aggregation

    • B.

      Composition

    • C.

      Encapsulation

    • D.

      Association

    Correct Answer
    B. Composition
    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.

    Rate this question:

  • 3. 

    What is true about constructors?

    • A.

      It can contain return type

    • B.

      It can take any number of parameters

    • C.

      It can have any non access modifiers

    • D.

      Constructor cannot throw an exception

    Correct Answer
    B. It can take any number of parameters
    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.

    Rate this question:

  • 4. 

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

    • A.

      Compilation error

    • B.

      Runtime error

    • C.

      Compilation and runs successfully

    • D.

      Only String return type is allowed

    Correct Answer
    A. Compilation error
    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.

    Rate this question:

  • 5. 

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

    • A.

      JVM

    • B.

      JDK

    • C.

      JIT

    • D.

      JRE

    Correct Answer
    B. JDK
    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.

    Rate this question:

  • 6. 

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

    • A.

      JVM

    • B.

      JDK

    • C.

      JIT

    • D.

      JRE

    Correct Answer
    A. JVM
    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.

    Rate this question:

  • 7. 

    What is the extension of java code files?

    • A.

      .class

    • B.

      .java

    • C.

      .txt

    • D.

      .js

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

    Rate this question:

  • 8. 

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

    • A.

      Malloc

    • B.

      Alloc

    • C.

      New

    • D.

       give

    Correct Answer
    C. New
    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.

    Rate this question:

  • 9. 

    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);         }      }

    • A.

       1

    • B.

       2

    • C.

      Runtime error

    • D.

      Garbage value

    Correct Answer
    A.  1
    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.

    Rate this question:

  • 10. 

    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);         }      }

    • A.

      0

    • B.

      1

    • C.

       Runtime error

    • D.

      Classname@hashcode in hexadecimal form

    Correct Answer
    D. Classname@hashcode in hexadecimal form
    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.

    Rate this question:

  • 11. 

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

    • A.

      Static

    • B.

       constant

    • C.

      Protected

    • D.

      Final

    Correct Answer
    D. Final
    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.

    Rate this question:

  • 12. 

    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();              }    }

    • A.

      1

    • B.

      2

    • C.

      3

    • D.

      4

    Correct Answer
    B. 2
    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.

    Rate this question:

  • 13. 

    What is the return type of a method that does not return any value?

    • A.

      Int

    • B.

      Float

    • C.

       void

    • D.

      Double

    Correct Answer
    C.  void
    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.

    Rate this question:

  • 14. 

    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);                 }       }  

    • A.

      0

    • B.

      1

    • C.

      6

    • D.

      25

    Correct Answer
    C. 6
    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.

    Rate this question:

  • 15. 

    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);         }      }

    • A.

      1

    • B.

      2

    • C.

      Runtime Error

    • D.

      Compilation Error

    Correct Answer
    B. 2
    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.

    Rate this question:

  • 16. 

    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);   }   

    • A.

      0

    • B.

      Garbage value

    • C.

      Compiler error

    • D.

       runtime error

    Correct Answer
    C. Compiler error
    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.

    Rate this question:

  • 17. 

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

    • A.

      Super

    • B.

      This

    • C.

      Extent

    • D.

      Extends

    Correct Answer
    D. Extends
    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.

    Rate this question:

  • 18. 

    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();              }    }

    • A.

      0

    • B.

      1

    • C.

      2

    • D.

      Compilation Error

    Correct Answer
    C. 2
    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.

    Rate this question:

  • 19. 

    All classes in Java are inherited from which class?

    • A.

      Java.lang.class

    • B.

       java.class.inherited

    • C.

      Java.class.object

    • D.

      Java.lang.Object

    Correct Answer
    D. Java.lang.Object
    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.

    Rate this question:

  • 20. 

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

    • A.

      Protected

    • B.

      Private

    • C.

       Public

    • D.

      Static

    Correct Answer
    B. Private
    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.

    Rate this question:

  • 21. 

    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);                 }      }

    • A.

      10 20

    • B.

      20 10

    • C.

      20 40

    • D.

      40 20

    Correct Answer
    A. 10 20
    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.

    Rate this question:

  • 22. 

    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);              }     }

    • A.

      6

    • B.

      7

    • C.

      8

    • D.

      9

    Correct Answer
    C. 8
    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.

    Rate this question:

  • 23. 

    What is the extension of compiled java classes?

    • A.

      .class

    • B.

      .java

    • C.

      .txt

    • D.

      .js

    Correct Answer
    A. .class
    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.

    Rate this question:

  • 24. 

     Which of these can be overloaded?

    • A.

      Methods

    • B.

      Constructors

    • C.

      All of the mentioned

    • D.

      None of the mentioned

    Correct Answer
    C. All of the mentioned
    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.

    Rate this question:

  • 25. 

    What is the data type of any class you write?

    • A.

      Primitive

    • B.

      Non primitive

    • C.

      String

    • D.

      None

    Correct Answer
    B. Non primitive
    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.

    Rate this question:

  • 26. 

    How many primitive data types does java contain?

    • A.

      8

    • B.

      6

    • C.

      9

    • D.

      None

    Correct Answer
    A. 8
    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.

    Rate this question:

  • 27. 

    How many methods does the Object class contain?

    • A.

      12

    • B.

      11

    • C.

      13

    • D.

      10

    Correct Answer
    B. 11
    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.

    Rate this question:

  • 28. 

    JVM can understand which type of file?

    • A.

      .class

    • B.

      .java

    • C.

      Both a & c

    • D.

      None of the above

    Correct Answer
    A. .class
    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.

    Rate this question:

  • 29. 

    What are the components of a platform?

    • A.

      OS+ram

    • B.

      OS+processor

    • C.

      OS+processor+rom

    • D.

      None of the above

    Correct Answer
    B. OS+processor
    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.

    Rate this question:

Related Topics

Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.