Can You Pass This Java Test? Quiz

Approved & Edited by ProProfs Editorial Team
At ProProfs Quizzes, our dedicated in-house team of experts takes pride in their work. With a sharp eye for detail, they meticulously review each quiz. This ensures that every quiz, taken by over 100 million users, meets our standards of accuracy, clarity, and engagement.
Learn about Our Editorial Process
| Written by 323890
3
323890
Community Contributor
Quizzes Created: 1 | Total Attempts: 166
Questions: 20 | Attempts: 166

SettingsSettingsSettings
Can You Pass This Java Test? Quiz - Quiz

.


Questions and Answers
  • 1. 

    What is the output for the below code ? public class Test {                 public static void main(String[] args) {                                 List list = new ArrayList();                                 list.add(0, 59);                                 int total = list.get(0);                                 System.out.println(total);} }

    • A. 

      A)59

    • B. 

      B)Compile time error, because you have to do int total = ((Integer)(list.get(0))).intValue();

    • C. 

      C)Compile time error, because can't add primitive type in List.

    • D. 

      D)Compile Properly but Runtime Exception

    Correct Answer
    A. A)59
    Explanation
    The code creates an ArrayList object and adds the integer value 59 at index 0. Then, it retrieves the value at index 0 and assigns it to the variable 'total'. Finally, it prints the value of 'total', which is 59. Therefore, the output of the code is 59.

    Rate this question:

  • 2. 

    Class A {             Try             {                         Class.forName(“jdbc:odbc:DriverManager”);             }             Catch(Exception e)             {             } }  

    • A. 

      1.jdbc:odbc:Driver Manager just Load the Driver 2.Load and initialize the Driver 3.load,initialize and register the driver 1.jdbc:odbc:Driver Manager just Load the Driver 2.Load and initialize the Driver 3.load,initialize and register the driver 1.jdbc:odbc:Driver Manager just Load the Driver 2.Load and initialize the Driver 3.load,initialize and register the driver 1.jdbc:odbc:Driver Manager just Load the Driver 2.Load and initialize the Driver 3.load,initialize and register the driver 1.jdbc:odbc:Driver Manager just Load the Driver 2.Load and initialize the Driver 3.load,initialize and register the driver 1.jdbc:odbc:Driver Manager just Load the Driver 2.Load and initialize the Driver 3.load,initialize and register the driver 1.jdbc:odbc:Driver Manager just Load the Driver 2.Load and initialize the Driver 3.load,initialize and register the driver 1.jdbc:odbc:Driver Manager just Load the Driver 2.Load and initialize the Driver 3.load,initialize and register the driver 1.jdbc:odbc:Driver Manager just Load the Driver 2.Load and initialize the Driver 3.load,initialize and register the driver 1.jdbc:odbc:Driver Manager just Load the Driver 2.Load and initialize the Driver 3.load,initialize and register the driver 1.jdbc:odbc:Driver Manager just Load the Driver 2.Load and initialize the Driver 3.load,initialize and register the driver 1.jdbc:odbc:Driver Manager just Load the Driver 2.Load and initialize the Driver 3.load,initialize and register the driver 1.jdbc:odbc:Driver Manager just Load the Driver 2.Load and initialize the Driver 3.load,initialize and register the driver 1.jdbc:odbc:Driver Manager just Load the Driver 2.Load and initialize the Driver 3.load,initialize and register the driver 1.jdbc:odbc:Driver Manager just Load the Driver 2.Load and initialize the Driver 3.load,initialize and register the driver 1.jdbc:odbc:Driver Manager just Load the Driver 2.Load and initialize the Driver 3.load,initialize and register the driver 1.jdbc:odbc:Driver Manager just Load the Driver 2.Load and initialize the Driver 3.load,initialize and register the driver jdbc:odbc:Driver Manager just Load the Driver

    • B. 

      Load and initialize the Driver

    • C. 

      Load,initialize and register the driver

    Correct Answer
    C. Load,initialize and register the driver
    Explanation
    The correct answer is "load, initialize and register the driver". In the given code, the Class.forName method is used to load the JDBC driver class. This method not only loads the driver class into the memory, but also initializes and registers it with the DriverManager. Therefore, the correct answer is option 3.

    Rate this question:

  • 3. 

    Select the true statements in the following

    • A. 

      Runtime exceptions can be handled through catch with errors

    • B. 

      Runtime exceptions need not have throws declaration

    • C. 

      If throws is given, then try catch block should be used

    • D. 

      Runtime exceptions can be handled if it is extended by error class

    Correct Answer
    B. Runtime exceptions need not have throws declaration
    Explanation
    Runtime exceptions in Java are unchecked exceptions, meaning that they do not need to be declared using the "throws" keyword in the method signature or caught using a try-catch block. This is because these exceptions are not checked by the compiler at compile-time. Unlike checked exceptions, such as IOException, which must be declared or caught, runtime exceptions can be handled or left unhandled without any compile-time error. Therefore, the statement "Runtime exceptions need not have throws declaration" is true.

    Rate this question:

  • 4. 

    Statement A:Anonymous class can extend class and implement interface at a time     Statement B:Anonymous class can have own members

    • A. 

      A is true,B is false

    • B. 

      A is false,B is true

    • C. 

      A is true,B is true

    • D. 

      A is false,B is false

    Correct Answer
    B. A is false,B is true
    Explanation
    The correct answer is A is false, B is true. Anonymous classes can only extend a class or implement an interface, but not both at the same time. However, they can have their own members, including fields, methods, and constructors.

    Rate this question:

  • 5. 

    Class A {             Try//line 1             {//line 2                         //Statement   //line 3 }//line 4 }    

    • A. 

      Shows uncaught exceptions at line 4

    • B. 

      Shows uncaught exceptions at line 5

    • C. 

      Demands finally block at line 4

    • D. 

      Demands finally block at line 5

    Correct Answer
    B. Shows uncaught exceptions at line 5
  • 6. 

    1. Vector 2.HashSet 3.TreeSet 4.ArrayList i)Null Values ii)when getting values, sorted and stored iii)not ordered and sorted iv)Capacity            

    • A. 

      1-iii, 2-iv, 3-ii, 4-i

    • B. 

      1-i, 2-ii, 3-iii, 4-iv

    • C. 

      1-iv , 2-iii, 3-ii, 4-i

    • D. 

      1-iv, 2-iii, 3-i, 4-ii

    Correct Answer
    C. 1-iv , 2-iii, 3-ii, 4-i
    Explanation
    The correct answer is 1-iv, 2-iii, 3-ii, 4-i. This means that Vector has a capacity, HashSet is not ordered and sorted, TreeSet gets values sorted and stored, and ArrayList allows null values.

    Rate this question:

  • 7. 

    1)final 2)abstract 3)private i)objects in subclass cant be access these ii)can’t override iii)cant instantiate

    • A. 

      1 - i , 2 - iii , 3 - ii

    • B. 

      1 - ii , 2 - i , 3 - iii

    • C. 

      1 - i, 2 - ii , 3 - iii

    • D. 

      1 – ii , 2 – iii , 3 – i

    Correct Answer
    D. 1 – ii , 2 – iii , 3 – i
    Explanation
    The correct answer is 1 - ii, 2 - iii, 3 - i. This means that the explanation for the correct answer is as follows: "final" keyword in Java is used to prevent a class from being subclassed or a method from being overridden. Therefore, the statement "2 - iii" is correct because "abstract" methods can only be defined in abstract classes and cannot be instantiated. The statement "3 - i" is correct because private access modifier restricts access to the member only within the same class.

    Rate this question:

  • 8. 

    Class TestReturn{       Public static void main(string args[]) {              Int i=10;             SOP(“Result : ”+ getSquare(i));               }  Public static int getSquare (int i)                        {                Return i*i;              SOP(“END OF GET SQUARE”);                                 } }

    • A. 

      A)100

    • B. 

      B)runtime error

    • C. 

      C) END OF GET SQUARE , 100

    • D. 

      D) 100 , END OF GET SQUARE

    • E. 

      E) compile error

    Correct Answer
    E. E) compile error
    Explanation
    The given code has a compile error. This is because the method getSquare is called inside the SOP method, but the SOP method is not defined in the code. The correct method name should be System.out.println instead of SOP.

    Rate this question:

  • 9. 

    Set of services provided by component or class is called as

    • A. 

      A.interface

    • B. 

      B.container

    • C. 

      C.component

    • D. 

      D.object

    Correct Answer
    D. D.object
    Explanation
    The correct answer is d.object. In object-oriented programming, an object is an instance of a class that encapsulates data and behavior. It represents a real-world entity and can have properties (data) and methods (behavior). A set of services provided by a component or class can be considered as the services provided by the object. Therefore, the correct answer is d.object.

    Rate this question:

  • 10. 

    A. Employee(){} b.Public Employee(){} c.Private Employee(){} i)no argument constructor ii)does not allow instantiation outside package iii)does not allow instantiation inside package

    • A. 

      1.a-i,b-ii,c-iii

    • B. 

      2.a-ii,b-iii,c-i

    • C. 

      3.a-iii,b-ii,c-i

    • D. 

      4)b-i , a- ii , c - iii

    Correct Answer
    D. 4)b-i , a- ii , c - iii
    Explanation
    The correct answer is 4) b-i, a-ii, c-iii. This means that option b corresponds to no argument constructor, option a corresponds to does not allow instantiation outside package, and option c corresponds to does not allow instantiation inside package.

    Rate this question:

  • 11. 

    Which of the following best matches the example of television

    • A. 

      1.Encapsulation

    • B. 

      2.polymorphism

    • C. 

      3.Inheritance

    Correct Answer
    A. 1.Encapsulation
    Explanation
    Encapsulation is the concept of bundling data and methods together into a single unit, known as a class, in object-oriented programming. This allows for data hiding and ensures that the internal state of an object is protected from external interference. Television can be seen as an example of encapsulation because it encapsulates various features such as a screen, speakers, remote control, and internal circuitry within a single device. These features are hidden from the user, who can interact with the television through a simplified interface.

    Rate this question:

  • 12. 

    Public class Eat { Eat(){System.out.println(“Eat”);} class chocolate{ chocolate(){System.out.println(“chocolate”);} public void eat() {System.out.println(“Fruit”);} }             public static void main(String a[])             {             Eat f=new Eat();              f.method(); } Public void method() {   new  chocolate(){}.eat(); } }

    • A. 

      1.eat chocolate fruit

    • B. 

      2.compilation error

    • C. 

      3.Runtime error

    • D. 

      4.eat fruit

    Correct Answer
    A. 1.eat chocolate fruit
    Explanation
    The correct answer is 1. eat chocolate fruit. When the main method is called, it creates an instance of the Eat class and then calls the method() method. Inside the method() method, a new instance of the chocolate class is created using an anonymous inner class. This anonymous inner class has a method called eat() which prints "Fruit". Therefore, when the eat() method is called on the chocolate instance, it prints "Fruit". So the output will be "Fruit".

    Rate this question:

  • 13. 

    Consider the following code: public class Code17 { public static void main(String args[]) { new Code17(); } { System.out.print("Planet "); }  { System.out.print("Welcome "); } } Which of the following will be the valid output for the above code?  

    • A. 

      A. Compilation Error

    • B. 

      B.Compiles and Executes with no output

    • C. 

      C. Planet

    • D. 

      D. Welcome Planet

    • E. 

      E. Planet Welcome

    Correct Answer
    E. E. Planet Welcome
    Explanation
    The code defines a class named Code17 with a main method. Inside the class, there are two instance initializer blocks, one printing "Planet" and the other printing "Welcome". When the main method is called, it creates an object of the Code17 class, which triggers the execution of the instance initializer blocks. Therefore, the output will be "Planet Welcome".

    Rate this question:

  • 14. 

    1. public class Foo { 2. public static void main(String[] args) { 3. try { 4. return; 5. } finally { 6. System.out.println( "Finally" ); 7. } 8. } 9. } What is the result?

    • A. 

      A. Finally

    • B. 

      B. Compilation fails.

    • C. 

      C. The code runs with no output

    • D. 

      D. An exception is thrown at runtime

    Correct Answer
    A. A. Finally
    Explanation
    The code will run and the output will be "Finally" because the finally block is always executed, regardless of whether an exception is thrown or not. In this case, the try block does not throw an exception and the return statement is executed, but the finally block is still executed afterwards.

    Rate this question:

  • 15. 

    1.enum Animals{ 2.DOG("woof"),CAT("meow"),FISH("burble"); 3.String sound; 4.Animals(String s) { sound=s;} 5.} 6.class TestEnum{ 7.static Animals a; 8.public static void main(String[] args){ 9.System.out.println(a.DOG.sound+" "+a.FISH.sound); 10.} 11.}

    • A. 

      A.woof burble.

    • B. 

      B.multiple compilation errors.

    • C. 

      C.Compilation fails due to an error on line 2.

    • D. 

      D.Compilation fails due to an error on line 3

    • E. 

      E.Compilation fails due to an error on line 4.

    • F. 

      F.Compilation fails due to an error on line 9

    Correct Answer
    A. A.woof burble.
    Explanation
    The code defines an enum called Animals with three values: DOG, CAT, and FISH. Each value has a corresponding sound. In the main method, it prints the sound of the DOG value followed by a space and then the sound of the FISH value. Since the sound for DOG is "woof" and the sound for FISH is "burble", the output will be "woof burble". Therefore, the correct answer is a.woof burble.

    Rate this question:

  • 16. 

    Which declare a compilable abstract class?

    • A. 

      A.public abstract class Canine{public Bark speak();}

    • B. 

      B.public abstract class Canine{public Bark speak(){}}

    • C. 

      C.public class Canine{public abstract Bark speak();}

    • D. 

      D.public class canine abstract{public abstract Bark speak();}

    Correct Answer
    B. B.public abstract class Canine{public Bark speak(){}}
    Explanation
    This answer is correct because it declares a compilable abstract class named "Canine" with a method named "speak" that returns a type "Bark". The method body is empty, which is allowed for an abstract method.

    Rate this question:

  • 17. 

    1.enumA{A} 2.class E2{ 3.enum B{B} 4.void C(){ 5.enum D{ D} 6.} 7.}

    • A. 

      A.The code compiles

    • B. 

      B.If only line 1 is removed the code compiles

    • C. 

      C.If only line3 is removed the code compiles

    • D. 

      D.If only line 5 is removed the code compiles.

    • E. 

      E.If lines 1 and 3 are removed the code compiles.

    • F. 

      F.If lines 1,3 and 5 are removed the code compiles

    Correct Answer(s)
    D. D.If only line 5 is removed the code compiles.
    F. F.If lines 1,3 and 5 are removed the code compiles
    Explanation
    The code compiles because it follows the correct syntax for defining an enum and a class. If only line 5 is removed, the code will still compile because the enum D is defined within the method C and is not being accessed outside of it. If lines 1, 3, and 5 are removed, the code will still compile because the enum A and B are not being used anywhere in the code.

    Rate this question:

  • 18. 

    protected Hmpf h; } class Woop extends Zing{ } class Hmpf{ } Which is true?

    • A. 

      A.Woop is-a Hmpf and has-a Zing

    • B. 

      B.Zing is-a Woop and has-a Hmpf

    • C. 

      C.Hmpf has-a Woop and Woop is-a Zing.

    • D. 

      D.Woop has-a Hmpf and Woop is-a Zing

    • E. 

      E.Zing has-a Hmpf and Zing is-a Woop.

    Correct Answer
    D. D.Woop has-a Hmpf and Woop is-a Zing
  • 19. 

    Class Uber{ static int y=2; Uber(int x) { this(); y=y*2; } Uber() { y++; } class Minor extends Uber{ Minor() { super(y); y=y+3; } public static void main(String[] args) { new Minor(); System.out.println(y); } } What is the result?

    • A. 

      6

    • B. 

      7

    • C. 

      8

    • D. 

      9

    • E. 

      Compilation fails

    • F. 

      An exception is thrown.

    Correct Answer
    D. 9
    Explanation
    The code creates a class hierarchy where the class "Minor" extends the class "Uber". The "Uber" class has two constructors - one with a parameter and one without. The constructor with a parameter calls the constructor without a parameter using the "this()" keyword and then multiplies the static variable "y" by 2. The constructor without a parameter increments the static variable "y" by 1. The "Minor" class constructor calls the super constructor with the value of "y", which is 3 at that point, and then increments "y" by 3. Finally, in the main method, an object of the "Minor" class is created, which triggers the constructor calls. After that, the value of "y" is printed, which is 9.

    Rate this question:

  • 20. 

    Which statement is true?

    • A. 

      Has-a relationship always rely on inheritance.

    • B. 

      Has-a relationship always rely on instance variables.

    • C. 

      Has-a relationship always require atleast two class types.

    • D. 

      Has-a relationship always rely on polymorphism.

    • E. 

      Has-a relationships are always tightly coupled.

    Correct Answer
    B. Has-a relationship always rely on instance variables.
    Explanation
    The correct answer is "Has-a relationship always rely on instance variables." In object-oriented programming, a "has-a" relationship refers to the composition or aggregation of objects, where one object contains another object as a member variable. This allows the containing object to have access to the functionality and properties of the contained object. Instance variables are used to represent the "has-a" relationship between objects, as they hold the reference to the contained object. Inheritance, class types, and polymorphism are not necessary for a "has-a" relationship, but the use of instance variables is essential.

    Rate this question:

Back to Top Back to top
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.