Java Quiz For Job Interview

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 Vijayaprakash
V
Vijayaprakash
Community Contributor
Quizzes Created: 1 | Total Attempts: 344
Questions: 25 | Attempts: 344

SettingsSettingsSettings
Interview Quizzes & Trivia

Java Quiz for Job Interview


Questions and Answers
  • 1. 

    Public class Swap { public static void swapStrings(String x, String y){ String temp = x; x=y; y=temp; } public static void main(String[] args) { String a = "1"; String b = "2"; swapStrings(a, b); System.out.println("a="+a+" ,b="+b); }} What will be the output when executing this main?

    • A.

      An exception will be thrown

    • B.

      “a=2 ,b=1”

    • C.

      “a=1 ,b=2” because strings are immutable

    • D.

      “a=1 ,b=2” because Java passes parameters by value

    Correct Answer
    D. “a=1 ,b=2” because Java passes parameters by value
    Explanation
    The output will be "a=1 ,b=2" because Java passes parameters by value. In the swapStrings method, the values of x and y are swapped, but this does not affect the values of a and b in the main method. Java passes the values of the variables to the method, rather than the actual variables themselves. Therefore, the values of a and b remain unchanged in the main method and the output is "a=1 ,b=2".

    Rate this question:

  • 2. 

    Class Parent{ protected void x(){} public void y(){}}public class Child extends Parent{ public void x(){} protected void y(){}}

    • A.

      It compiles successfully

    • B.

      Compilation error – x can’t have its visibility increased

    • C.

      Compilation error – y can not have its visibility reduced

    • D.

      Compilation error – neither x nor y can have their visibility changed

    Correct Answer
    C. Compilation error – y can not have its visibility reduced
    Explanation
    The given code compiles successfully because the visibility of a method in a subclass can be the same or less restrictive than the visibility in the superclass. In this case, the method "y" in the subclass has protected visibility, which is less restrictive than the public visibility in the superclass. Therefore, there is no compilation error for reducing the visibility of method "y".

    Rate this question:

  • 3. 

    Following code will result in: int a = 3.5;

    • A.

      Compilation error

    • B.

      Runtime error

    • C.

      A being 3.5

    • D.

      A being 3

    Correct Answer
    A. Compilation error
    Explanation
    The code will result in a compilation error because the variable "a" is declared as an integer, but it is being assigned a floating-point value (3.5). In Java, you cannot directly assign a floating-point value to an integer variable without explicit casting.

    Rate this question:

  • 4. 

    Following code will result in: int a1 = 5; double a2 = (float) a1;

    • A.

      Compilation error

    • B.

      Runtime error

    • C.

      No errors

    Correct Answer
    C. No errors
    Explanation
    The code will not result in any errors. It initializes an integer variable "a1" with the value 5, and then assigns this value to a double variable "a2" after casting it to a float. Since the casting is allowed and both int and float can be assigned to a double without any loss of precision, there are no compilation or runtime errors.

    Rate this question:

  • 5. 

    Following code will result in: class A {    int b = 1;    public static void main(String[] args) {        System.out.println("b is " + b);    }}

    • A.

      Compilation error

    • B.

      Runtime Error

    • C.

      Runtime Exception

    • D.

      B is 1

    Correct Answer
    A. Compilation error
    Explanation
    The given code will result in a compilation error. This is because the variable "b" is an instance variable and it cannot be accessed directly in a static method like the main method. To access the instance variable "b" in the main method, an object of class A needs to be created first.

    Rate this question:

  • 6. 

    Following code will result in: class A {    public static void main(String[] args) {        B b = new A();    }}class B extends A {}

    • A.

      Compile error

    • B.

      Runtime Exception

    • C.

      No error

    Correct Answer
    A. Compile error
    Explanation
    The given code will result in a compile error because the line "B b = new A();" is trying to assign an instance of class A to a variable of type B. Since B is a subclass of A, it is not possible to assign an instance of a superclass to a variable of a subclass type without explicit type casting. This violates the principle of inheritance and results in a compile error.

    Rate this question:

  • 7. 

    Following code will result in: class A {    public static void main(String[] args) {        A a = new B();    }}class B extends A {}

    • A.

      Compiler error

    • B.

      Runtime Exception

    • C.

      No errors

    Correct Answer
    C. No errors
    Explanation
    The given code will not result in any errors because class B is a subclass of class A. Therefore, it is valid to create an object of class B and assign it to a reference variable of type A. This is known as polymorphism, where an object of a subclass can be treated as an object of its superclass.

    Rate this question:

  • 8. 

    Methods that are marked protected can be called in any subclass of that class

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
    Explanation
    Protected methods in a class can be accessed by any subclass of that class. This means that if a class has a protected method, any subclass that inherits from that class can call and use that method. This allows for better encapsulation and code organization, as subclasses can inherit and use the protected methods defined in the parent class without exposing them to the outside world.

    Rate this question:

  • 9. 

    An abstract class can have non-abstract methods.

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
    Explanation
    An abstract class can have non-abstract methods because an abstract class is a class that cannot be instantiated and is meant to be extended by other classes. It can contain both abstract and non-abstract methods. Non-abstract methods in an abstract class can have a complete implementation and can be directly called by the subclasses that extend the abstract class. This allows the abstract class to provide common functionality to its subclasses while also allowing the subclasses to implement their own unique functionality.

    Rate this question:

  • 10. 

    What is an instanceof

    • A.

      A method in object

    • B.

      An operator and keyword

    Correct Answer
    B. An operator and keyword
    Explanation
    The given correct answer is "An operator and keyword". The instanceof keyword in Java is used to check if an object is an instance of a particular class, a subclass, or a class that implements a specific interface. It returns a boolean value, true if the object is an instance of the specified class or interface, and false otherwise. It is commonly used in conditional statements or in determining the type of an object at runtime.

    Rate this question:

  • 11. 

    Can you compare a boolean to an integer?

    • A.

      True

    • B.

      False

    Correct Answer
    B. False
    Explanation
    A boolean represents a logical value, either true or false. An integer, on the other hand, represents a whole number. Since they represent different types of values, it is not possible to directly compare a boolean to an integer.

    Rate this question:

  • 12. 

    If class A implements an interface does it need to implement all methods of that interface?

    • A.

      Yes, always

    • B.

      No, not when A is abstract

    Correct Answer
    B. No, not when A is abstract
    Explanation
    When a class A implements an interface, it is required to implement all the methods of that interface. However, if class A is declared as abstract, it is not mandatory to implement all the methods of the interface. Abstract classes are designed to be extended by other classes, and these subclasses can provide the implementation for the remaining methods of the interface. This allows for more flexibility in the design and implementation of the abstract class and its subclasses.

    Rate this question:

  • 13. 

    Integer a = new Integer(2); Integer b = new Integer(2); What happens when you do if (a==b)?

    • A.

      Compiler error

    • B.

      Runtime Exception

    • C.

      True

    • D.

      False

    Correct Answer
    D. False
    Explanation
    When comparing the objects 'a' and 'b' using the '==' operator, it checks if the two objects refer to the same memory location. In this case, since 'a' and 'b' are created using the 'new' keyword, they are separate objects with different memory locations, even though they have the same value. Therefore, the comparison will result in 'False'.

    Rate this question:

  • 14. 

    Synchronized is a keyword to tell a Thread to grab an Object lock before continuing execution.

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
    Explanation
    The statement is true because the keyword "synchronized" in Java is used to ensure that only one thread can access a block of code or a method at a time. When a thread encounters a synchronized block or method, it acquires the lock on the object associated with that block or method, preventing other threads from accessing it simultaneously. This ensures thread safety and helps avoid race conditions and data inconsistencies.

    Rate this question:

  • 15. 

    Which ones does not extend java.lang.Number(you can make multiple choices)

    • A.

      Integer

    • B.

      Boolean

    • C.

      Character

    • D.

      Long

    • E.

      Short

    Correct Answer(s)
    B. Boolean
    C. Character
    Explanation
    The classes Boolean and Character do not extend the java.lang.Number class. The java.lang.Number class is the superclass of classes that represent numeric values in Java, such as Integer, Long, and Short. However, Boolean represents a boolean value (true or false) and Character represents a single character. They are not numeric types and therefore do not extend the Number class.

    Rate this question:

  • 16. 

    Which JDBC method is used to execute any SQL statement with a "SELECT" clause, that return the result of the query as a result set.

    • A.

      ExecuteUpdate()

    • B.

      ExecuteQuery()

    • C.

      Execute()

    Correct Answer
    B. ExecuteQuery()
    Explanation
    The correct answer is executeQuery(). This method is used to execute any SQL statement with a "SELECT" clause and returns the result of the query as a result set. The executeQuery() method is specifically designed for retrieving data from the database and is commonly used when executing SELECT statements. It returns a ResultSet object that contains the data retrieved from the database, which can be processed further to retrieve the desired information.

    Rate this question:

  • 17. 

    Which JDBC method is used to execute INSERT, DELETE, UPDATE , and other SQL DDL such as CREATE, DROP TABLE.

    • A.

      ExecuteUpdate()

    • B.

      ExecuteQuery()

    • C.

      Execute()

    Correct Answer
    A. ExecuteUpdate()
    Explanation
    The correct answer is executeUpdate(). This method is used to execute SQL statements that modify the data in the database, such as INSERT, DELETE, UPDATE, CREATE, and DROP TABLE. It returns an integer value representing the number of rows affected by the SQL statement. This method is commonly used for executing SQL DDL (Data Definition Language) statements.

    Rate this question:

  • 18. 

    Which JDBC method is used for retrieving a string value (SQL type VARCHAR) and assigning into java String object.

    • A.

      GetVarchar()

    • B.

      GetObject()

    • C.

      GetString()

    Correct Answer
    C. GetString()
    Explanation
    The correct answer is getString(). This method is used in JDBC for retrieving a string value (SQL type VARCHAR) from a database and assigning it into a Java String object. It is specifically designed to handle string data and ensures that the retrieved value is converted into a Java String object for further manipulation or processing in the application.

    Rate this question:

  • 19. 

    This method is used for retrieving the value from current row as object.

    • A.

      GetRow()

    • B.

      GetObject()

    • C.

      GetString()

    Correct Answer
    B. GetObject()
    Explanation
    The getObject() method is used to retrieve the value from the current row as an object. This means that the method returns the value in its original data type, such as Integer, String, or Date, without any conversion. It is commonly used when the specific data type of the value is not known or when it needs to be handled dynamically.

    Rate this question:

  • 20. 

    The Connection.prepareStatement() method accepts a SQL query and returns a...

    • A.

      PreparedStatement object

    • B.

      CallableStatement object

    • C.

      PrepareCall method

    Correct Answer
    A. PreparedStatement object
    Explanation
    The Connection.prepareStatement() method is used to create a PreparedStatement object, which represents a precompiled SQL statement. This object can be used to execute the SQL statement multiple times with different parameter values. It allows for efficient execution of SQL queries and provides protection against SQL injection attacks. Therefore, the correct answer is PreparedStatement object.

    Rate this question:

  • 21. 

    This method returns an integer value indicating a row count.

    • A.

      ExecuteQuery()

    • B.

      ExecuteUpdate()

    • C.

      Execute()

    Correct Answer
    B. ExecuteUpdate()
    Explanation
    The executeUpdate() method is used to execute SQL statements that modify the data in a database. It returns an integer value indicating the number of rows affected by the statement. Therefore, it is the most appropriate method to use when you want to retrieve the row count after executing a SQL statement.

    Rate this question:

  • 22. 

    Consider the following classpublic class Test implements Runnable{    public void run() {    }}Creating an instance of this class and calling its run() method will spawn a new thread.

    • A.

      True

    • B.

      False

    Correct Answer
    B. False
    Explanation
    Creating an instance of the Test class and calling its run() method will not spawn a new thread. In order to create a new thread, the Test class needs to be passed as a parameter to the Thread constructor and then the start() method needs to be called on the thread object.

    Rate this question:

  • 23. 

    What is the result of attempting to compile and run the following code?public class Test {    public static void main(String[] args){        Integer a = new Integer(4);        Integer b = new Integer(8);        Set hs = new HashSet();        hs.add(a);        hs.add(b);        hs.add(a);        System.out.println(hs.size());    }}

    • A.

      It prints: 2

    • B.

      It prints: 3

    Correct Answer
    A. It prints: 2
    Explanation
    The code creates two Integer objects, "a" with a value of 4 and "b" with a value of 8. It then creates a HashSet object called "hs" and adds "a" and "b" to it. However, since HashSet does not allow duplicate values, when "a" is added again, it is not added to the HashSet. Therefore, the size of the HashSet is 2, and when the code prints the size using the "System.out.println()" method, it prints 2.

    Rate this question:

  • 24. 

    What is the result of attempting to compile and run the following code?public class Test {    public static void main(String[] args){        Integer a = new Integer(4);        Integer b = new Integer(8);        Integer c = new Integer(4);        Set hs = new HashSet();        hs.add(a);        hs.add(b);        hs.add(c);        System.out.println(hs.size());    }}

    • A.

      It prints: 2

    • B.

      It prints: 3

    Correct Answer
    A. It prints: 2
    Explanation
    The code creates three Integer objects with values 4, 8, and 4. It then adds these objects to a HashSet. Since HashSet does not allow duplicate values, only two unique values (4 and 8) will be stored in the HashSet. Therefore, when we print the size of the HashSet using the `hs.size()` method, it will return 2.

    Rate this question:

  • 25. 

    Class Figure {    public void print() {        System.out.println("FIGURE");    }}class Triangle extends Figure {    public void print() {        System.out.println("TRIANGLE");    }}public class Test {    public static void main(String[] args) {        Figure f1 = new Figure();        f1.print();        Figure f2 = new Triangle();        f2.print();        Triangle t = new Triangle();        t.print();    }}What will this program print out?

    • A.

      FIGURE FIGURE FIGURE

    • B.

      FIGURE FIGURE TRIANGLE

    • C.

      FIGURE TRIANGLE TRIANGLE

    Correct Answer
    C. FIGURE TRIANGLE TRIANGLE
    Explanation
    The program will print out "FIGURE TRIANGLE TRIANGLE". This is because when the print() method is called on the objects f1, f2, and t, the method in the Triangle class is executed instead of the method in the Figure class. This is because f2 and t are both instances of the Triangle class, so the overridden method in Triangle is invoked. Therefore, the output will be "FIGURE" for f1 and "TRIANGLE" for f2 and t.

    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 20, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • Nov 03, 2012
    Quiz Created by
    Vijayaprakash
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.