Sematec Java Programming Quiz

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 Asgari.sematec
A
Asgari.sematec
Community Contributor
Quizzes Created: 8 | Total Attempts: 9,926
Questions: 25 | Attempts: 421

SettingsSettingsSettings
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


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

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

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

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

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

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

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

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

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

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

    Rate this question:

  • 11. 

    Can you compare a boolean to an integer?

    • A.

      True

    • B.

      False

    Correct Answer
    B. False
    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.

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

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

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

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

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

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

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

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

    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 then be used to efficiently execute the SQL statement multiple times with different parameter values. 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. 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.

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

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

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

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

    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 21, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • Apr 19, 2017
    Quiz Created by
    Asgari.sematec
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.