Q1. Under what situations do you obtain a default constructor?
1. When the class has no other constructors //Answer
2. When you define at least one constructor
Select the most appropriate answer.
Q2. Given the following code:
public class Test
{
…
}
Which of the following can be used to define a constructor for this class:
1. public void Test() {…}
2. public Test() {…} //Answer
3. public static Test() {…}
4. public static void Test() {…}
Q3 Which of the following is a legal return type of a method overloading the following method:
public void add(int a) {…}
1. void
2. int
3. Can be anything//ans
Q4 Which of the following statements is correct for a method which is overriding the following method:
public void add(int a) {…}
1. the overriding method must return void //ans
2. the overriding method must return int
3. the overriding method can return whatever it likes
Q5.
public class Test
{
public static void test()
{
this.print();
}
public static void print()
{
System.out.println("Test");
}
public static void main(String args [])
{
test();
}
}
What is the result of compiling and running this class?
1. The string Test is printed to the standard out.
2. A runtime exception is raised stating that an object has not been created.
3. Nothing is printed to the standard output.
4. An exception is raised stating that the method test cannot be found.
5. An exception is raised stating that the non static this cannot be reference from static context//ans
6. The class fails to compile stating that the variable this is undefined.
Q6. Carefully examine the following code:
public class StaticTest
{
Static
{
System.out.println("Hi there");
}
public void print()
{
System.out.println("Hello");
}
public static void main(String args [])
{
StaticTest st1 = new StaticTest();
st1.print();
StaticTest st2 = new StaticTest();
st2.print();
}
}
When will the string "Hi there" be printed?
1. Never.
2. Each time a new instance is created.
3. Once when the class is first loaded into the Java virtual machine. //ans
4. Only when the static method is called explicitly.
Q7. Can you create instantiate a System class if Yes How? And if No Why?
Q8. What is the return type of a program’s main() method? //void
Q9What restrictions are placed on method overloading? type. //different signature
Q10 If a method is declared as protected, where may the method be accessed?//immediate subclass only
Q11 What is the difference between a field variable and a local variable?
Q13) Q6. Carefully examine the following code:
public class StaticTest
{
{
System.out.println("Non static block");
}
Static
{
System.out.println("Hi there");
}
public void print()
{
System.out.println("Hello");
}
public static void main(String args [])
{
StaticTest st1 = new StaticTest();
st1.print();
StaticTest st2 = new StaticTest();
st2.print();
}
1. Is a class a subclass itself? yes
2. What is the difference between a while statement and a do statement?
3. How are this() and super() used with constructors?
4. What are the legal operands of the instanceof operator? //left operand is the object and right operand is the class
5. If an object is garbage collected, can it become reachable again? //free method gets call which deallocates memory
6. What happens when you add a double value to a String? //concanetation or append string
7. Is sizeof a keyword? //no
8. Can an object’s finalize() method be invoked while it is reachable////no
9. What is the difference amongst JVM Spec, JVM Implementation, JVM Runtime ?
//Blueprint designed for the JVM is JVM specification
//actual implementation of the JVM
//at the time of execution instance of the JVM as per the native OS(platform) is JVM runtime
}