Java Quiz For Job Interview 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 Aldo.bongio
A
Aldo.bongio
Community Contributor
Quizzes Created: 1 | Total Attempts: 4,998
Questions: 25 | Attempts: 5,000

SettingsSettingsSettings
Java Quiz For Job Interview Questions - Quiz

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 of the main method will be "a=1 ,b=2" because Java passes parameters by value. In the swapStrings method, the values of the variables 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, not the actual variables themselves. Therefore, any changes made to the parameters inside the method do not affect the original variables in the main method.

    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
    In Java, when a method is overridden in a child class, the visibility of the method in the child class cannot be more restrictive than the visibility of the method in the parent class. In this case, the method "y()" in the parent class is declared as public, while in the child class it is declared as protected. Since protected is less restrictive than public, it violates the rule and results in a compilation error. Therefore, the correct answer is "Compilation error – y can not have its visibility reduced".

    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 given code will result in a compilation error because it is trying to assign a floating-point value (3.5) to an integer variable (a). In many programming languages, including C++, this is not allowed without an explicit type conversion. The code should either declare 'a' as a float or use a type cast to convert the floating-point value to an integer before assigning it to 'a'.

    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 because it is a valid code. It assigns the value 5 to the integer variable a1 and then casts it to a float, which is then assigned to the double variable a2. Since the casting is allowed and no other errors are present, the code will compile and run without any issues.

    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 because the variable "b" is an instance variable and it is being accessed in a static context (inside the main method) without creating an instance of class A. To fix this error, either the variable "b" should be declared as static or an instance of class A should be created to access the variable.

    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 code will result in a compile error because class A is trying to create an instance of class B using the statement "B b = new A();". This is not allowed because class A is not a subclass of class B. To fix this error, the statement should be changed to "A b = new B();".

    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. It creates an object of class B using the reference variable of class A. This is possible because class B is a subclass of class A, and in Java, a subclass object can be assigned to a superclass reference variable. Therefore, the code will compile and run without any errors.

    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 method is marked as protected in a superclass, it can be called and used in any subclass that extends that superclass. This allows for the subclass to inherit and utilize the protected methods from the superclass, providing a way to reuse code and extend functionality. Therefore, the statement that protected methods can be called in any subclass of that class is true.

    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 inherited by other classes. It can contain both abstract and non-abstract methods. Non-abstract methods in an abstract class can have a complete implementation, meaning they have a body and provide functionality. These non-abstract methods can be inherited by the subclasses and can also be overridden if needed. This allows the abstract class to provide a common set of methods and functionality to its subclasses while also allowing specific implementations to be added in the subclasses.

    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 "instanceof" is both an operator and a keyword in programming. It is used to check whether an object belongs to a particular class or implements a specific interface. The "instanceof" operator returns true if the object is an instance of the specified class or a subclass of it, and false otherwise. It is commonly used in object-oriented programming languages to perform type checking and make decisions based on the type of an object.

    Rate this question:

  • 11. 

    Can you compare a boolean to an integer?

    • A.

      True

    • B.

      False

    Correct Answer
    B. False
    Explanation
    A boolean is a data type that can have only two values, true or false. An integer, on the other hand, is a data type that represents whole numbers. Comparing a boolean to an integer would not make logical sense as they are different data types and have different value ranges. Therefore, the correct answer is false.

    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 class A is abstract, it means that it cannot be instantiated and can only be used as a base class for other classes. Abstract classes can implement an interface without providing implementations for all the methods defined in the interface. The responsibility of implementing the remaining methods is delegated to the concrete subclasses that inherit from the abstract class. Therefore, the correct answer is that class A does not need to implement all methods of the interface when it is abstract.

    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 two Integer objects using the "==" operator, it checks if the two objects refer to the same memory location. In this case, since a and b are two separate Integer objects, even though they have the same value of 2, they are not the same object in memory. Therefore, the condition "if (a==b)" will evaluate to 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 particular block of code or method at a time. When a thread encounters a synchronized block, it tries to acquire the lock on the object associated with that block. If the lock is already held by another thread, the current thread will wait until the lock is released. This ensures that the code inside the synchronized block is executed by only one thread at a time, preventing any concurrency issues.

    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
    Boolean and Character do not extend java.lang.Number. The java.lang.Number class is the superclass of all numeric wrapper classes in Java, such as Integer, Long, and Short. However, Boolean and Character are not numeric types and therefore do not extend Number.

    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 executeQuery() method is used to execute any SQL statement with a "SELECT" clause. It returns the result of the query as a result set, which can then be used to retrieve and manipulate the data returned by the query. This method is specifically designed for executing SELECT statements and is not suitable for executing statements that modify the database.

    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 database, such as INSERT, DELETE, UPDATE, and other SQL DDL statements like CREATE and DROP TABLE. It returns an integer value representing the number of rows affected by the SQL statement.

    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 getString() method in JDBC is used for retrieving a string value (SQL type VARCHAR) from a database and assigning it into a Java String object. This method is specifically designed for retrieving string values and is commonly used when working with VARCHAR columns in a database.

    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 it can be used to retrieve any type of value from the row, such as integers, strings, or even custom objects. This method is useful when the specific type of the value is not known beforehand or when the value can be of different types in different rows. By using getObject(), the value can be retrieved as an object and then casted to the appropriate type as needed.

    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 is a more efficient way to execute SQL queries that are executed repeatedly, as the statement is only compiled once. 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 the database. It returns an integer value indicating the number of rows affected by the query. This can be useful to determine the success of an SQL statement and to track the number of rows that have been updated, inserted, or deleted. The executeQuery() method is used to execute SQL statements that retrieve data from the database, and it returns a ResultSet object. The execute() method can be used to execute any type of SQL statement and returns a boolean value indicating whether the statement has returned a ResultSet or not.

    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. The Test class implements the Runnable interface, but it does not override the run() method. Therefore, when the run() method is called on an instance of the Test class, it will simply execute the empty run() method defined in the Runnable interface. To spawn a new thread, the Test class should be used as a parameter to the Thread constructor and then the start() method should be called on the created 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 a HashSet object and adds two Integer objects (a and b) to it. However, it adds the Integer object "a" twice. Since a HashSet does not allow duplicate elements, the second attempt to add "a" is ignored. Therefore, the size of the HashSet is 2, and the code will print "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 respectively. These objects are then added to a HashSet. However, since HashSet does not allow duplicate values, only two unique values (4 and 8) will be added to the set. Therefore, when the size of the HashSet is printed, it will output 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 object f1, which is an instance of the Figure class, it prints "FIGURE". When the print() method is called on the object f2, which is an instance of the Triangle class, it still prints "TRIANGLE" because the print() method in the Triangle class overrides the print() method in the Figure class. Finally, when the print() method is called on the object t, which is also an instance of the Triangle class, it again prints "TRIANGLE".

    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
  • Sep 29, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • Apr 29, 2010
    Quiz Created by
    Aldo.bongio
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.