JAVA QUIZZ 1
- What will be the output when executing this main?
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);
}
}
- An exception will be thrown
- “a=2 ,b=1”
- “a=1 ,b=2” because strings are immutable
- "a=1 ,b=2" because Java passes parameters by value (careful no return)
- What happens with following program?
class Parent{
protected void x(){}
public void y(){}
}
public class Child extends Parent{
public void x(){}
protected void y(){}
}
- It compiles successfully
- Compilation error – x can’t have its visibility increased
- Compilation error – y can not have its visibility reduced
- Compilation error – neither x nor y can have their visibility changed
- Following code will result in:
int a = 3.5;
- Compilation error
- Runtime error
- A being 3.5
- A being 3
- Following code will result in:
int a1 = 5;
double a2 = (float) a1;
- Compilation error
- Runtime error
- No errors : By default, Java uses double to represent its floating-point numerals. float is 32 bits whereas double is 64
- Following code will give:
class A {
int b = 1;
public static void main(String[] args) {
System.out.println("b is " + b);
}
}
- Compilation error (non static field cannot be referenced from a static context)
- Runtime Error
- Runtime Exception
- b is 1
- Following code will result in:
class A {
public static void main(String[] args) {
B b = new A();
}
}
class B extends A {
}
- Compile error
- Runtime Exception
- No error
- Following code will result in:
class A {
public static void main(String[] args) {
A a = new B();
}
}
class B extends A {
}
- Compiler error
- Runtime Exception
- No error
- Methods that are marked protected can be called in any subclass of that class
- True
- False
- Can you compare a boolean to an integer?
- True
- False
- An abstract class can have non-abstract methods.
- True
- False
- What is an instanceof
- A method in object
- An operator and keyword
- If class A implements an interface does it need to implement all methods of that interface?
- Yes, always
- No, not when A is abstract
- Which JDBC method is used to execute INSERT, DELETE, UPDATE , and other SQL DDL such as CREATE, DROP TABLE.
- executeUpdate()
- executeQuery()
- execute()
- Which JDBC method is used for retrieving a string value (SQL type VARCHAR) and assigning into java String object.
- getVarchar()
- getObject()
- getString()
- This method is used for retrieving the value from current row as object.
- getRow()
- getObject()
- getString()
- What happens when you do if (a==b)?
Integer a = new Integer(2);
Integer b = new Integer(2);
- Compiler error
- Runtime Exception
- True
- False
- Synchronized is a keyword to tell a Thread to grab an Object lock before continuing execution.
- True
- False
- Which ones does not extend java.lang.Number ? (you can make multiple choices)
- Integer
- Boolean
- Character
- Long
- Short
- 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.
- ExecuteUpdate()
- ExecuteQuery()
- Execute()
- Which JDBC method is used to execute INSERT, DELETE, UPDATE , and other SQL DDL such as CREATE, DROP TABLE.
- ExecuteUpdate()
- ExecuteQuery()
- Execute()
- Which JDBC method is used for retrieving a string value (SQL type VARCHAR) and assigning into java String object.
- GetVarchar()
- GetObject()
- GetString()
- This method is used for retrieving the value from current row as object.
- GetRow()
- GetObject()
- GetString()
- The Connection.prepareStatement() method accepts a SQL query and returns a...
- PreparedStatement object
- CallableStatement object
- PrepareCall method
- This method returns an integer value indicating a row count.
- executeQuery()
- executeUpdate()
- execute()
- 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:
- True
- False
An application that creates an instance of Thread must provide the code that will run in that thread. There are two ways to do this:
- Provide a Runnable object. The Runnable interface defines a single method, run, meant to contain the code executed in the thread. The Runnable object is passed to the Thread constructor, as in the HelloRunnable example:
public class HelloRunnable implements Runnable {
public void run() {
System.out.println("Hello from a thread!");
}
public static void main(String args[]) {
(new Thread(new HelloRunnable())).start();
}
}
- Subclass Thread. The Thread class itself implements Runnable, though its run method does nothing. An application can subclass Thread, providing its own implementation of run, as in the HelloThread example:
public class HelloThread extends Thread {
public void run() {
System.out.println("Hello from a thread!");
}
public static void main(String args[]) {
(new HelloThread()).start();
}
}
- 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());
}
}
- It prints: 2
- It prints: 3
- 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());
}
}
- It prints: 2
- It prints: 3
- What will this program print out?
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();
}
}
- FIGURE FIGURE FIGURE
- FIGURE FIGURE TRIANGLE
- FIGURE TRIANGLE TRIANGLE