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?
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.
2.
Class Parent{ protected void x(){} public void y(){}}public class Child extends Parent{ public void x(){} protected void y(){}}
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.
3.
Following code will result in: int a = 3.5;
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.
4.
Following code will result in: int a1 = 5; double a2 = (float) a1;
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.
5.
Following code will result in: class A { int b = 1; public static void main(String[] args) { System.out.println("b is " + b); }}
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.
6.
Following code will result in: class A { public static void main(String[] args) { B b = new A(); }}class B extends A {}
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.
7.
Following code will result in: class A { public static void main(String[] args) { A a = new B(); }}class B extends A {}
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.
8.
Methods that are marked protected can be called in any subclass of that class
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.
9.
An abstract class can have non-abstract methods.
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.
10.
What is an instanceof
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.
11.
Can you compare a boolean to an integer?
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.
12.
If class A implements an interface does it need to implement all methods of that interface?
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".
13.
Integer a = new Integer(2); Integer b = new Integer(2); What happens when you do if (a==b)?
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.
14.
Synchronized is a keyword to tell a Thread to grab an Object lock before continuing execution.
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.
15.
Which ones does not extend java.lang.Number(you can make multiple choices)
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.
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.
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.
17.
Which JDBC method is used to execute INSERT, DELETE, UPDATE , and other SQL DDL such as CREATE, DROP TABLE.
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.
18.
Which JDBC method is used for retrieving a string value (SQL type VARCHAR) and assigning into java String object.
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.
19.
This method is used for retrieving the value from current row as object.
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.
20.
The Connection.prepareStatement() method accepts a SQL query and returns a...
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.
21.
This method returns an integer value indicating a row count.
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.
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.
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.
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()); }}
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".
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()); }}
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.
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?
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".