Java Quiz For Job Interview Questions

Reviewed by Editorial Team
The ProProfs editorial team is comprised of experienced subject matter experts. They've collectively created over 10,000 quizzes and lessons, serving over 100 million users. Our team includes in-house content moderators and subject matter experts, as well as a global network of rigorously trained contributors. All adhere to our comprehensive editorial guidelines, ensuring the delivery of high-quality content.
Learn about Our Editorial Process
| By Aldo.bongio
A
Aldo.bongio
Community Contributor
Quizzes Created: 1 | Total Attempts: 5,240
| Attempts: 5,240 | Questions: 25
Please wait...
Question 1 / 25
0 %
0/100
Score 0/100
1. Synchronized is a keyword to tell a Thread to grab an Object lock before continuing execution.

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.

Submit
Please wait...
About This Quiz
Java Quiz For Job Interview Questions - Quiz

This Java Quiz for Job Interview assesses key programming concepts relevant for job seekers. It covers string manipulation, inheritance, data types, and scope, testing understanding of Java fundamentals... see morecrucial for technical interviews. see less

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

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.

Submit
3. An abstract class can have non-abstract methods.

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.

Submit
4. 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());
    }
}

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

Submit
5. 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.

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.

Submit
6. 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?

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

Submit
7. Can you compare a boolean to an integer?

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.

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

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.

Submit
9. Following code will result in:

int a1 = 5;
double a2 = (float) a1;

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.

Submit
10. Following code will result in:

class A {
    public static void main(String[] args) {
        A a = new B();
    }
}

class B extends A {
}

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.

Submit
11. What is an instanceof

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.

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

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.

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

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.

Submit
14. Following code will result in:

int a = 3.5;

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

Submit
15. Following code will result in:

class A {
    public static void main(String[] args) {
        B b = new A();
    }
}

class B extends A {
}

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();".

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

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.

Submit
17. Integer a = new Integer(2);
Integer b = new Integer(2);

What happens when you do if (a==b)?

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.

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

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.

Submit
19. 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());
    }
}

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.

Submit
20. Following code will result in:
class A {
    int b = 1;

    public static void main(String[] args) {
        System.out.println("b is " + b);
    }
}

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.

Submit
21.
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?

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.

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

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.

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

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.

Submit
24.
class Parent{
protected void x(){}
public void y(){}
}

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

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

Submit
25. This method returns an integer value indicating a row count.

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.

Submit
View My Results

Quiz Review Timeline (Updated): Sep 29, 2023 +

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
Cancel
  • All
    All (25)
  • Unanswered
    Unanswered ()
  • Answered
    Answered ()
Synchronized is a keyword to tell a Thread to grab an Object lock...
Methods that are marked protected can be called in any subclass of...
An abstract class can have non-abstract methods.
What is the result of attempting to compile and run the following...
Which JDBC method is used to execute any SQL statement with a "SELECT"...
Class Figure {    public void print()...
Can you compare a boolean to an integer?
The Connection.prepareStatement() method accepts a SQL query and...
Following code will result in: int a1 = 5; double a2 = (float) a1;
Following code will result in: class A {    public...
What is an instanceof
Which JDBC method is used for retrieving a string value (SQL type...
Which ones does not extend java.lang.Number(you can make multiple...
Following code will result in: int a = 3.5;
Following code will result in: class A {    public...
Which JDBC method is used to execute INSERT, DELETE, UPDATE , and...
Integer a = new Integer(2); Integer b = new Integer(2); What happens...
If class A implements an interface does it need to implement all...
What is the result of attempting to compile and run the following...
Following code will result in: class A {    int b =...
Public class Swap { public static void swapStrings(String x, String...
This method is used for retrieving the value from current row as...
Consider the following classpublic class Test implements...
Class Parent{ protected void x(){} public void y(){}}public class...
This method returns an integer value indicating a row count.
Alert!

Advertisement