Java Quiz For Job Interview

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 Vijayaprakash
V
Vijayaprakash
Community Contributor
Quizzes Created: 1 | Total Attempts: 359
| Attempts: 359 | Questions: 25
Please wait...
Question 1 / 25
0 %
0/100
Score 0/100
1. 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 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.

Submit
Please wait...
About This Quiz
Java Programming Quizzes & Trivia

This Java Quiz for Job Interview assesses key programming skills crucial for software development roles. It includes questions on string manipulation, inheritance, data types, and static context, testing... see morethe candidate's understanding of Java fundamentals and object-oriented programming. see less

2. Can you compare a boolean to an integer?

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.

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

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

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

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

Submit
7. Following code will result in:

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

class B extends A {
}

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.

Submit
8. Following code will result in:

int a = 3.5;

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.

Submit
9. 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 allows for efficient execution of SQL queries and provides protection against SQL injection attacks. Therefore, the correct answer is PreparedStatement object.

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

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

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.

Submit
12. Following code will result in:

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

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.

Submit
13. What is an instanceof

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.

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

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.

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

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

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

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

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

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.

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

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

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

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

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

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

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

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

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

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

Submit
View My Results

Quiz Review Timeline (Updated): Mar 20, 2023 +

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

Advertisement