Sematec Java Programming Quiz

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 Asgari.sematec
A
Asgari.sematec
Community Contributor
Quizzes Created: 8 | Total Attempts: 10,457
| Attempts: 426 | 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 A has a protected method, it can be called by any subclass B that extends class A. This allows for inheritance and allows subclasses to access and use the protected methods defined in their superclass. Hence, the statement "Methods that are marked protected can be called in any subclass of that class" is true.

Submit
Please wait...
About This Quiz
Sematec Java Programming Quiz - Quiz

The 25 questions most important parts of the course tested Dhdagr 70 earn a lower score, you need to study this lesson

2. 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 JDBC method used to execute any SQL statement with a "SELECT" clause and return the result of the query as a result set is executeQuery(). This method is specifically designed for executing SELECT statements and retrieving the data from the database. It returns a ResultSet object that contains the rows and columns of the query result, which can then be processed further in the application.

Submit
3. 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 then be used to efficiently execute the SQL statement multiple times with different parameter values. Therefore, the correct answer is PreparedStatement object.

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, since HashSet does not allow duplicate elements, when the code tries to add the second occurrence of object "a" to the HashSet, it is not added because it is already present. Therefore, the size of the HashSet remains 2, and the code prints "2".

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

Explanation

The statement is true because the "synchronized" keyword 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 or method, it grabs the object lock associated with that block or method, allowing it to execute without interference from other threads. This helps in preventing race conditions and maintaining thread safety in multi-threaded environments.

Submit
6. What is an instanceof

Explanation

The answer is "An operator and keyword" because instanceof is a binary operator in Java that is used to check if an object is an instance of a particular class, an instance of a subclass, or an instance of a class that implements a specific interface. It returns true if the object is an instance of the specified type, otherwise it returns false. The keyword "instanceof" is used in conjunction with the operator to perform this type checking operation.

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

Explanation

A boolean value represents either true or false, while an integer represents a whole number. These two data types are not directly comparable because they are fundamentally different. In programming, it is not valid to compare a boolean to an integer using the comparison operators, as they operate on values of the same type. Therefore, the correct answer is false.

Submit
8. 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, whereas abstract methods do not have an implementation and must be overridden by the subclasses. This allows the abstract class to provide common functionality to its subclasses while also allowing the subclasses to implement their own unique behavior.

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

Explanation

The JDBC method used for retrieving a string value (SQL type VARCHAR) and assigning it into a Java String object is getString(). This method is specifically designed to retrieve string values from the database and assign them to a Java String object. It ensures that the retrieved value is properly converted and stored as a string in the Java program.

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 because it follows the principle of inheritance. Class B extends class A, so an object of class B can be assigned to a reference variable of class A. Therefore, the code will compile and run without any issues.

Submit
11. 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 assignment statement. The value of the integer variable "a1" is being explicitly casted to a float, and then assigned to the double variable "a2". Since the value being assigned is a valid float value, there will be no compilation or runtime errors.

Submit
12. 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 the value assigned to it (3.5) is a floating-point number. In Java, you cannot directly assign a floating-point number to an integer variable without explicit type casting.

Submit
13. 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, and other SQL DDL statements like CREATE and DROP TABLE. It returns an integer value representing the number of rows affected by the execution of the statement.

Submit
14. 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, it belongs to the Figure class and therefore prints "FIGURE". When the print method is called on the object f2, it is a Figure object but refers to a Triangle object, so it still prints "TRIANGLE". Finally, when the print method is called on the object t, which is a Triangle object, it prints "TRIANGLE".

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

Explanation

When a class A is abstract, it means that it cannot be instantiated and can only serve as a blueprint for other classes. In this case, if class A implements an interface, it does not necessarily need to implement all the methods of that interface. The responsibility of implementing the methods can be delegated to the concrete subclasses that extend class A. Therefore, the correct answer is "No, not when A is abstract".

Submit
16. 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 elements, only two elements (4 and 8) will be added to the set. Therefore, when the size of the HashSet is printed, it will be 2.

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

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

Explanation

When comparing objects using the "==" operator, it checks if the two objects refer to the same memory location. In this case, the objects "a" and "b" are both Integer objects created using the new keyword, so they will refer to different memory locations. Therefore, the condition "a==b" will evaluate to false.

Submit
18. 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 abstract superclass of classes that represent numeric values and it provides common functionality for all the subclasses. However, Boolean and Character are not numeric types, so they do not extend the Number class.

Submit
19. 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 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 create an instance of a class, the class being instantiated must either be the same class or a subclass of the class doing the instantiation. Therefore, the code will not compile.

Submit
20. 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. It can be used when the data type of the value is unknown or when it can be of different types. This method returns the value as an Object, which can then be casted to the appropriate data type if needed.

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

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

Explanation

In this code, the class Child is inheriting from the class Parent. The method x() in the Parent class is protected, which means it can be accessed by subclasses. In the Child class, the method x() is being overridden and given public visibility, which is allowed. However, the method y() in the Parent class is public, and in the Child class, it is being overridden and given protected visibility. This is not allowed because the visibility of an overridden method cannot be reduced. Therefore, there is a compilation error – y cannot have its visibility reduced.

Submit
22.
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 the variables a and b are passed as parameters. However, Java passes parameters by value, which means that only the values of the variables are passed, not the actual variables themselves. Therefore, the swapStrings() method swaps the values of x and y, but it does not affect the values of a and b in the main method. As a result, when the main method prints the values of a and b, they remain unchanged.

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

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

Explanation

The code will result in a compilation error because the variable "b" is an instance variable and cannot be accessed directly from the static main method. To access the instance variable "b" within the static method, an object of class A needs to be created first.

Submit
24. 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 spawn a new thread, the Test class needs to implement the Runnable interface and then pass an instance of the Test class to a new Thread object. The Thread object will then create a new thread and call the run() method on the Test class. Since the Test class does not implement the Runnable interface in this given code, calling the run() method will not spawn a new thread.

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 a database. It returns an integer value indicating the number of rows affected by the statement. This method is typically used for INSERT, UPDATE, and DELETE statements. Therefore, it makes sense that it would return a row count as the answer. The executeQuery() method is used to execute SQL statements that retrieve data from a database, and the execute() method is a general-purpose method that can be used for both types of statements.

Submit
View My Results

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

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

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

Advertisement