Q1)What is the o/p of following program?
public class Question05 {
public static void main(String[] args) {
Question05Sub myref = new Question05Sub();
try{
myref.test();
}catch(IOException ioe){}
}
void test() throws IOException{
System.out.println("In Question05");
throw new IOException();
}
}
class Question05Sub extends Question05 {
void test() {
System.out.println("In Question05Sub");
}
}
a) In Question05
b) In Question05Sub
c) Compilation error
d) In Question05
In Question05Sub
Answer
Answer is c. If the parent method throws a Checked Exception then the child class overriding it can throw same exception or unchecked exception or any subclass of the Exception thrown (in this case subclass of IOException)
Q2) What is the o/p of following program?
public class ThreadClass {
public static void main(String[] args) {
new ThreadClass().doSomething();
}
public void doSomething(){
int i=5;
Thread t = new Thread(new Runnable(){
public void run(){
for(int j=0;j<=i;j++){
System.out.print(" "+j);
}
}
});
t.start();
}
}
a) Print 0,1,2,3,4
b) Print 1,2,3,4
c) Compilation error
d) throws RunTimeException
Answer
Q3)What is the o/p of following program?
public class Parent {
static {
System.out.println("Inside Parent static");
}
{
System.out.println("Inside Parent init");
}
public Parent(){
System.out.println("Parent Const");
}
public static void main(String args[]){
new MyChild();
}
}
class MyChild extends Parent{
static {
System.out.println("Inside Child static");
}
{
System.out.println("Inside Child init");
}
public MyChild(){
System.out.println("Child Const");
}
}
a) Inside Parent static
Inside Child static
Inside Parent init
Parent Const
Inside Child init
Child Const
b) Inside Parent static
Inside Child static
Inside Parent init
Inside Child init
Parent Const
Child Const
c) Inside Parent static
Inside Child static
Inside Parent init
Inside Child init
Child Const
Parent Const
d) Inside Parent init
Inside Child init
Inside Parent static
Inside Child static
Child Const
Parent Const
Answer
Answer is a.
Q4)What is the o/p of following program?
public class Test {
public static void main(String args[]){
int i = 132;
short s = 15;
byte b = (byte)i;
int x = b + s;
System.out.println(x);
}
}
a) 147
b) -109
c) Compilation error
d) Rutime error
Answer
Q5)What is o/p of following program?
public class Test {
public static void main(String args[]){
int i = 132;
List list = new ArrayList();
list.add(new Object());
list.add("Hi");
list.add(i);
System.out.println(list.get(1));
}
}
a) Hi
b)Compilation Error
c) Runtime Error
d) 132
Answer
Answer is a.
Q6)Which of the o/p following program?
public class StringTest {
public static void main(String[] args) {
String String = "String"; //line 1
int temp = 2; //line 2
Object:for(int main=0;main
System.out.print(String.charAt(main)+" "); //line 4
if(main>temp) //line 5
break Object; //line 6
}
}
}
a) Prints S r n
b) Prints t i g
c) Compilation Error
d) Prints t g
Answer
Answer is a.
Q7) Select methods that correctly overload the following method
byte bMethod(short i) throws Exception {...}
a) int bMethod(int i) throws IOException{...}
b) protected int bMethod(short s) throws FileNotFoundException{...}
c) private String aMethod(byte b,short s) throws Exception{...}
d) char bMethod(String s) throws RuntimeException{...}
e) int bMethod(short sh){...}
Answer
Answer is a,c,d.
Q8) What is o/p of following program?
public class FinalVar {
private int final i;
public static void main(String args[]){
System.out.println(new FinalVar().i);
}
}
a) 1
b) 0
c) RunTimeException occurs
d) Compile time error 'i should be initialized'
Answer
Answer is d.final variable need to be declared always even the default value should be mentioned explicitly
Part - 3
Q1)What is the o/p of following program?
public class Widening1 {
public void f1(Object o1){
System.out.println("Inside f1 with object as argument");
}
public void f1(String s){
System.out.println("Inside f1 with String as argument");
}
public static void main(String[] args) {
new Widening1().f1(null);
}
}
a) Inside f1 with String as argument
b) Inside f1 with object as argument
c) Compilation error
d) Runtime error
Answer
Answer is a. Since the parameter passed to function is null so a function which has parameter less broad or wide is called.In this case it is String as String extends Object and has less scope.
Q2) What is the o/p of following program?
public class Widening1 {
public void f1(Object o1){
System.out.println("Inside f1 with object as argument");
}
public void f1(String s){
System.out.println("Inside f1 with String as argument");
}
public void f1(String s){
System.out.println("Inside f1 with Integer as argument");
}
public static void main(String[] args) {
new Widening1().f1(null);
}
}
a) Inside f1 with String as argument
b) Inside f1 with object as argument
c) Compilation error
d) Inside f1 with object as argument
Answer
Answer is c, Since the parameter passed to function is null so a function which has parameter less broad or wide is called.In this case String and Integer is at same level and hence causes ambiguity.
Q3)What is the o/p of following program?
public class MyClass{
public static void main(String[] args) {
int[] dest = new int[]{0,1,2,3,4,5};
System.out.println(dest[0]+ dest[5]+dest[2]);
}
}
a) 052
b) Compilation Error
c) 7
d)152
Answer
Answer is c. If you are caught with this, then try running it ...
Q4)What is the o/p of following program?
public class Overloading {
public void f1(Integer i){
System.out.println("inside 1");
}
public void f1(int i){
System.out.println("inside 2");
}
public static void main(String args[]){
new Overloading().f1(8);
}
}
a) inside 1
b) inside 2
c) Compilation error
d) Rutime error
Answer
Answer is b.
Q5)Wil this code compile fine?
public class FinalHashMap {
1 private static final Map map = new HashMap();
2 public static void main(String[] args) {
3 map.put("param1","value1");
4 map.put("param2","value2");
5 map=new HashMap();
}
}
a) Yes
b)No,Compilation Error at line 3
c)No, Runtime Error
d)No,Compilation Error at line 5
Answer
Answer is d.Once declared the final variable cannot be initialized again
Q6)Which of the o/p following program?
public class FinalHashMap {
1 private final Map map = new HashMap();
2 public static void main(String[] args) {
3 map.put("param1","value1");
4 map.put("param2","value2");
5 map=new HashMap();
}
}
a) Yes
b)No,Compilation Error at line 3
c)No, Runtime Error
d)No,Compilation Error at line 5
Answer
Answer is b. Non static variable cannot be used inside static variable.
Q7) What is o/p of following program
public class Parent {
public static void staticMethod(){
System.out.println("Inside Parent static");
}
public static void main(String[] args) {
Parent p = new Child();
p.staticMethod();
}
}
class Child extends Parent {
public static void staticMethod(){
System.out.println("Inside Child static");
}
}
a) Inside Parent static
b) Inside Child static
c) Compilte time error.
Answer
Answer is a. Static method cannot be overriden.
Q8) Is it compulsory to implement print() method in abstract class?
public interface Interface1 {
void print(String str);
}
public abstract class AbstractClass implements Interface1 {
}
Answer
Answer is No.
No comments:
Post a Comment
Happy Reading!