Java (Programming Language) Quiz Questions

20 Questions | Total Attempts: 166

SettingsSettingsSettings
Please wait...
Java (Programming Language) Quiz Questions

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

  • 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 */} }

  • 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

  • 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

  • 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

  • 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

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

  • 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

  • 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

  • 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

  • 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

  • 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

  • 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

  • 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

  • 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

  • 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

  • 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

  • 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

  • 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.

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