Java (Programming Language) Quiz Questions

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 Paramu
P
Paramu
Community Contributor
Quizzes Created: 1 | Total Attempts: 1,484
Questions: 20 | Attempts: 1,484

SettingsSettingsSettings
Java (Programming Language) Quiz Questions - Quiz

This is a Java programming language quiz. Take this practice quiz and learn more codings of java.


Questions and Answers
  • 1. 

    Which of the following statement is true regarding constructors? Which of the following statement is true regarding constructors?

    • A.

      Default Constructors are optional only for the classes that does not have constructors

    • B.

      Default Constructors are Optional for all classes

    • C.

      Can be overloaded across inherited classes

    • D.

      Abstract classes cannot have constructors

    Correct Answer
    A. Default Constructors are optional only for the classes that does not have constructors
    Explanation
    Default constructors are optional only for the classes that do not have constructors because if a class does not have any constructor defined, the compiler automatically provides a default constructor for that class. However, if a class already has a constructor defined, the default constructor is not automatically provided and must be explicitly defined if needed.

    Rate this question:

  • 2. 

    Consider the following code: public abstract class Shape { private int x; private int y; public abstract void draw(); public void setAnchor(int x, int y) { this.x = x; this.y = y; } }  

    • A.

      A. public class Circle extends Shape { private int radius; public void draw();}

    • B.

      B. public abstract class Circle extends Shape { private int radius;}

    • C.

      Public class Circle extends Shape { private int radius; public void setRadius(int radius) { this.radius = radius; } public int getRadius() { return radius; } public void draw() {/* code here */}}

    • D.

      public class Circle implements Shape { private int radius;}

    • E.

      Public class Circle extends Shape { public int radius; private void draw() {/* code here */} }

    Correct Answer(s)
    C. Public class Circle extends Shape { private int radius; public void setRadius(int radius) { this.radius = radius; } public int getRadius() { return radius; } public void draw() {/* code here */}}
    E. Public class Circle extends Shape { public int radius; private void draw() {/* code here */} }
  • 3. 

    Which of the following options is true about multiple inheritance?    

    • A.

      Inheriting from two super classes

    • B.

      Inheriting from a class which is already in an inheritance hierarchy

    • C.

      Inheriting from more than one super class

    • D.

      Inheriting from a single class

    Correct Answer
    A. Inheriting from two super classes
    Explanation
    Multiple inheritance refers to the ability of a class to inherit attributes and behaviors from more than one parent class. In this case, the correct answer states that multiple inheritance involves inheriting from two super classes. This means that a class can inherit attributes and behaviors from two different parent classes simultaneously.

    Rate this question:

  • 4. 

    Consider the following code: import java.util.*; public class Code10 { { final Vector v; v=new Vector(); } public Code10() { } public void codeMethod() { System.out.println(v.isEmpty()); } public static void main(String args[]) { new Code10().codeMethod(); } }  

    • A.

      Prints: false

    • B.

      Runtime error: NullPointerException

    • C.

      Compilation error: cannot find the symbol

    • D.

      Compilation error: v is not initialised inside the constructor

    • E.

      Prints: true

    Correct Answer
    C. Compilation error: cannot find the symbol
    Explanation
    The correct answer is "Compilation error: cannot find the symbol". This is because the variable "v" is declared inside the constructor and is not accessible outside the constructor. Therefore, in the codeMethod() method, when trying to access the "v" variable, it cannot be found and a compilation error occurs.

    Rate this question:

  • 5. 

    Consider the following code: public class LabeledBreak2 { public static void main(String args[]) { loop: for(int j=0; j<2; j++) { for(int i=0; i<10; i++) { if(i == 5) break loop; System.out.print(i + " "); } } } } Which of the following will be the output for the above code?  

    • A.

      A. 0 1 2 3 4 5

    • B.

      B. Indefinite Loop

    • C.

      C. 1 2 3 4 5

    • D.

      D. 0 1 2 3 4

    • E.

      E. 0 1 2 3 4 0 1 2 3 4

    Correct Answer
    D. D. 0 1 2 3 4
    Explanation
    The code uses a labeled break statement "break loop" to break out of the outer loop when i equals 5. This means that the inner loop will only run until i equals 5, and the outer loop will only run once. Therefore, the output will be 0 1 2 3 4.

    Rate this question:

  • 6. 

    Consider the following scenario: Real Chocos Private Limited deals in manufacturing variety of chocolates. This organization manufactures three varieties of chocolates. 1. Fruit Chocolates 2. Rum Chocolates 3. Milk Chocolates A software system needs to be built. Which of the following options identifies the Classes and Objects?  

    • A.

      A. Class: Real Chocos Private Limited Objects: Chocolate

    • B.

      B. Class: Fruit Chocolates Objects: Rum Chocolates

    • C.

      C. Class: Chocolate Objects: Fruit Chocolates, Rum Chocolates, Milk Chocolates

    • D.

      D. Class: Choclate Objects: Milk Chocolates

    Correct Answer
    C. C. Class: Chocolate Objects: Fruit Chocolates, Rum Chocolates, Milk Chocolates
    Explanation
    In this scenario, the class represents the general category of "Chocolate" and the objects represent the specific varieties of chocolates that can be manufactured, which are "Fruit Chocolates", "Rum Chocolates", and "Milk Chocolates". Therefore, option c correctly identifies the classes and objects in this scenario.

    Rate this question:

  • 7. 

    Consider the following partial code: interface A { public int getValue(); } class B implements A { public int getValue() { return 1; } } class C extends B { // insert code here } Which of the following code fragments, when inserted individually at the commented line (// insert code here), makes use of polymorphism? (Choose 3)  

    • A.

      A. public void add(B b) { b.getValue(); }

    • B.

      B. public void add(A a) { a.getValue(); }

    • C.

      C. public void add(C c1, C c2) { c1.getValue(); }

    • D.

      D. public void add(C c) { c.getValue(); }

    • E.

      E. public void add(A a, B b) { a.getValue(); }

    Correct Answer(s)
    A. A. public void add(B b) { b.getValue(); }
    B. B. public void add(A a) { a.getValue(); }
    E. E. public void add(A a, B b) { a.getValue(); }
    Explanation
    The correct answer choices (a, b, and e) all involve methods that accept objects of different classes as parameters. This demonstrates polymorphism because objects of class B and class C are both subclasses of class A, so they can be treated as objects of type A. By accepting objects of type A or B as parameters, these methods can be used with objects of class B or class C, showing polymorphic behavior.

    Rate this question:

  • 8. 

    Which of the following are correct regarding HashCode?(Choose 2)  

    • A.

      A. it is a 32 bit numeric digest key

    • B.

      B. the numeric key is unique

    • C.

      C. hashCode() value cannot be a zero-value

    • D.

      D. It improves performance

    • E.

      E. hashCode() is defined in String class

    Correct Answer(s)
    D. D. It improves performance
    E. E. hashCode() is defined in String class
    Explanation
    The given answer states that HashCode improves performance and that hashCode() is defined in the String class. HashCode is a 32-bit numeric digest key that is used to improve performance in various data structures like hash tables. The hashCode() method is indeed defined in the String class and is used to generate the hash code value for a given string. The answer does not mention anything about the numeric key being unique or the hashCode() value not being a zero-value.

    Rate this question:

  • 9. 

    Which are all platform independent among the following? (Choose 3)  

    • A.

      A. Java Virtual Machine (JVM)

    • B.

      B. Java Source Files

    • C.

      C. Java Development Kit (JDK)

    • D.

      D. Java Class Files

    • E.

      E. JAR Files

    Correct Answer(s)
    B. B. Java Source Files
    D. D. Java Class Files
    E. E. JAR Files
    Explanation
    Java Source Files, Java Class Files, and JAR Files are all platform independent. Java Source Files are written in plain text and can be compiled and executed on any platform that has a Java Virtual Machine (JVM) installed. Java Class Files are generated by the Java compiler and contain bytecode that can be interpreted by the JVM, making them platform independent. JAR Files are archives that contain compiled Java classes and resources, and they can be run on any platform with a JVM. The JVM itself is not platform independent as it needs to be specifically implemented for each operating system.

    Rate this question:

  • 10. 

    Consider the following listed items: A. a method declared as final B. a method declared as abstract C. a method declared as private Consider the following statements: I.   Will not be available in sub classes II.  Will deny overriding the method III. Will not allow instantiating the class Which of the following option gives the exact matches of above listed items and statements?

    • A.

      A-II, B-III, C-I

    • B.

      A-III, B-II, C-I

    • C.

      A-I, B-II, C-III

    • D.

      A-II, B-I, C-III

    Correct Answer
    A. A-II, B-III, C-I
    Explanation
    Option A-II, B-III, C-I gives the exact matches of the listed items and statements. This means that a method declared as final will deny overriding the method, a method declared as abstract will not allow instantiating the class, and a method declared as private will not be available in sub classes.

    Rate this question:

  • 11. 

    Consider the following code: public class TestOverloading {    int _length(String s) {       return s.length();    }    float _length(String s) {       return (float) s.length();    } } Which of the following statement is true regarding the above code?

    • A.

      Both the length() methods are duplicated methods

    • B.

      Both the length() methods are overloaded methods

    • C.

      Overloaded methods cannot start with a special character like '_'

    • D.

      Overloaded methods should be declared as public

    Correct Answer
    A. Both the length() methods are duplicated methods
  • 12. 

    Consider the following code: class AllClass {    private static int i = 10;    static { i += 10; }    { i += 10; }    AllClass() { i += 10; }    AllClass incrementWith10() { i += 10; return this;} } public class AllAccess {    public static void main(String[] args) {       System.out.println(new AllClass().incrementWith10().i);    } } Which of the following option gives the output for the above code?

    • A.

      Compile time error

    • B.

      Prints: 40

    • C.

      Prints: 50

    • D.

      Run time error

    Correct Answer
    A. Compile time error
    Explanation
    The code will result in a compile time error because the variable "i" is declared as private in the AllClass class. This means that it can only be accessed within the same class and cannot be accessed in the main method of the AllAccess class. Therefore, the code will not compile and will result in a compile time error.

    Rate this question:

  • 13. 

    Which of the following options give the valid argument types for main() method? 1) String[] args 2) String args[] 3) String ..args 4) String args 5) String[] args[]

    • A.

      1,2,3

    • B.

      2,3,4

    • C.

      3,4,5

    • D.

      1,3,5

    • E.

      1,3,4

    Correct Answer
    A. 1,2,3
    Explanation
    The valid argument types for the main() method in Java are 1) String[] args, 2) String args[], and 3) String ..args. These options correctly represent the different ways in which the command-line arguments can be passed to the main() method. Option 1 uses the standard array syntax, option 2 uses an alternative array syntax, and option 3 uses varargs syntax to allow for a variable number of arguments.

    Rate this question:

  • 14. 

    Which of the following option gives one possible use of the statement 'the name of the public class should match with its file name'?

    • A.

      Helps the compiler to find the source file that corresponds to a class, when it does not find a class file while compiling

    • B.

      To maintain the uniform standard

    • C.

      Helps Javadoc to build the Java Documentation easily

    Correct Answer
    A. Helps the compiler to find the source file that corresponds to a class, when it does not find a class file while compiling
    Explanation
    The statement "the name of the public class should match with its file name" helps the compiler to find the source file that corresponds to a class when it does not find a class file while compiling. This ensures that the compiler can locate the correct file and compile the class successfully.

    Rate this question:

  • 15. 

    Which of the following statement is true?

    • A.

      Classes can be loaded at Runtime, without actually referring the class in the code at compile time.

    • B.

      Classes can be loaded at Runtime, but the name of the class with full package name should be given in the code at compile time.

    • C.

      Classes cannot be loaded at Runtime

    • D.

      Only class that is loaded at runtime is the class that contains the main() method

    Correct Answer
    A. Classes can be loaded at Runtime, without actually referring the class in the code at compile time.
    Explanation
    Classes can indeed be loaded at runtime without actually referring to the class in the code at compile time. This is possible through the use of mechanisms such as reflection in Java. Reflection allows for the dynamic loading and instantiation of classes at runtime, enabling the program to access and utilize classes that were not known or referenced during compilation. This flexibility is particularly useful in scenarios where the specific classes to be used may vary or change dynamically during the execution of the program.

    Rate this question:

  • 16. 

    Consider the following code: interface Declare {  Declaration 1:  protected int a = 5;           Declaration 2:  public static final int e = 9; Declaration 3:  volatile int c = 7;            Declaration 4:  transient int d = 8;           } Which of the following option gives the declarations that results in compilation error?

    • A.

      Declaration 1,3,4

    • B.

      Declaration 2,4

    • C.

      Declaration 1,2,3

    • D.

      Declaration 2,3,4

    Correct Answer
    A. Declaration 1,3,4
    Explanation
    Declaration 1,3,4 will result in compilation error because the access modifiers used (protected and volatile) are not allowed for variables in an interface. Interface variables are by default public, static, and final. Therefore, using protected and volatile for Declaration 1 and Declaration 3 respectively will cause a compilation error. Additionally, using transient for Declaration 4 is also not allowed in an interface, resulting in a compilation error.

    Rate this question:

  • 17. 

    Consider the following Statements: Statement A: Anonymous inner class can be created in initializer or static blocks Statement B: Anonymous inner class has no constructor Which of the following option is true regarding the above given statements?

    • A.

      Both Statements A and B are true

    • B.

      Statement A is false and B is true

    • C.

      Statement A is true and B is false

    • D.

      Both Statements A and B are false

    Correct Answer
    A. Both Statements A and B are true
    Explanation
    Both statements A and B are true. An anonymous inner class can be created in an initializer or static block, as well as in other places such as method arguments or expressions. Additionally, an anonymous inner class does not have a constructor because it is created and instantiated at the same time.

    Rate this question:

  • 18. 

    Consider the following code: Line 1:class A { Line 2:  void display() { } Line 3:} Line 4:class B extends A { Line 5:   // insert missing code here Line 6:} Which of the following options give the code snippets, when inserted individually at the line no 5, will correctly complete the definition of class B? 1) int display() { /* more code here */ }  2) void display() { /* more code here */ } 3) private void display() { /* more code here */ }  4) protected void display() { /* more code here */ }

    • A.

      2,4

    • B.

      1,2

    • C.

      2,3

    • D.

      3,4

    Correct Answer
    A. 2,4
    Explanation
    The correct answer is options 2 and 4. This is because class B is extending class A, which means it is inheriting all the methods and variables of class A. Therefore, in order to correctly complete the definition of class B, the missing code at line 5 should be the same method signature as the one in class A. Option 2, which is "void display() { /* more code here */ }", matches the method signature in class A. Option 4, which is "protected void display() { /* more code here */ }", also matches the method signature in class A and is a valid option.

    Rate this question:

  • 19. 

    consider the following code:  Line No:1 public class MovieRelease  Line No:2 { Line No:3 public static void main(String[] args) { Line No:4 class Movie { Line No:5 public String name; Line No:6 public Movie(String s) { Line No:7 name = s; Line No:8}} Line No:9 Object obj = new Movie("MaskOfZoro"); Line No:10 System.out.println(obj.name); Line No:11} } Which of the following option gives the valid output for the above code?

    • A.

      Zippo

    • B.

      An exception occurs at runtime at line 10.

    • C.

      Compilation fails because of an error in line 3.

    • D.

      Compilation fails because of an error in line 9.

    • E.

      Compilation fails because of an error in line 10.

    Correct Answer
    A. Zippo
    Explanation
    The code defines a class Movie with a constructor that takes a String parameter and assigns it to the instance variable name. In the main method, an object of the Movie class is created with the name "MaskOfZoro" and assigned to the variable obj. Finally, the name of the object is printed. Since the name of the object is "MaskOfZoro", the output will be "MaskOfZoro". Therefore, the valid output for the above code is "Zippo".

    Rate this question:

  • 20. 

    Consider the following code: Line no 1:class Outer { Line no 2:public static class Inner { Line no 3:} Line no 4:public static void display() { } } Line no 5:public class Test Line no 6:{ Line no 7:public static void main(String args[]) Line no 8:{ Line no 9:// Replace with code from the option below Line no 10:}} Which of the following option when replaced at line no 9, instantiates an instance of the nested class?

    • A.

      Outer.Inner o = new Outer.Inner();

    • B.

      Outer.Inner oi = new Inner();

    • C.

      "Outer o = new Outer(); Outer.Inner oi = o.new Outer.Inner();"

    • D.

      Inner oi = new Outer.Inner();

    Correct Answer
    A. Outer.Inner o = new Outer.Inner();
    Explanation
    The correct answer is "Outer.Inner o = new Outer.Inner();". This code instantiates an instance of the nested class "Inner" by using the syntax "Outer.Inner" to access the nested class and then using the "new" keyword to create a new instance of it. The instance is then assigned to the variable "o".

    Rate this question:

Quiz Review Timeline +

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
  • Mar 13, 2012
    Quiz Created by
    Paramu
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.