Methods and Constructors

Quiz Guidelines

 

Multiple Choice Question

QID: 
114

What will be the contents of s1 and s2 at the time of the println statement in the main method of the following program?

import java.util.*;
public class TestClass{
public static void main(String args[]){
Stack s1 = new Stack ();
Stack s2 = new Stack ();
processStacks (s1,s2);
System.out.println (s1 + " "+ s2);
}
public static void processStacks(Stack x1, Stack x2){
x1.push (new Integer ("100")); //assume that the method push adds the x2 = x1;
}
}

Select 1 option

A. [100] [100]
B. [100] []
C. [] [100]
D. [] []

Multiple Choice Question

QID: 
113

What will the following code print when compiled and run:

class Data {
    int intVal = 0;
    String strVal = "default";
    public Data(int k){
        this.intVal = k; 
    }
}
public class TestClass {
    public static void main(String[] args) throws Exception {
        Data d1 = new Data(10);
        d1.strVal = "D1";
        Data d2 = d1;
        d2.intVal = 20;
        System.out.println("d2 val = "+d2.strVal);
    }
}

Select 1 option

A. d2 val =
B. d2 val = default 
C. d2 val = D1
D. Exception at run time.

Multiple Choice Question

QID: 
112

Given:

class StaticTest{
    void m1(){
        StaticTest.m2();  // 1
        m4();             // 2
        StaticTest.m3();  // 3
    }
    static void m2(){ }  // 4
    void m3(){
        m1();            // 5
        m2();            // 6
        StaticTest.m1(); // 7
    }
    static void m4(){ }
}

Which of the lines will fail to compile?

Select 2 options

A. 1
B. 2
C. 3
D. 4
E. 5
F. 6
G. 7

Multiple Choice Question

QID: 
111

Identify valid method declarations.

Select 1 option

A. public void methodX(int... i, String s);
B. public void methodX(int... i, String... s);
C. public void methodX(int i, int... j);
D. public int... methodX(int i);

Multiple Choice Question

QID: 
110

What will the following program print?

public class InitTest{
   public InitTest(){
      s1 = sM1("1");
   }
   static String s1 = sM1("a");
   String s3 = sM1("2");{
      s1 = sM1("3");
   }
   static{
      s1 = sM1("b");
   }
   static String s2 = sM1("c");
   String s4 = sM1("4");
    public static void main(String args[]){
        InitTest it = new InitTest();
    }
    private static String sM1(String s){
       System.out.println(s);  return s;
    }
}

Select 1 option

A. The program will not compile. 
B. It will print : a b c 2 3 4 1 
C. It will print : 2 3 4 1 a b c
D. It will print : 1 a 2 3 b c 4
E. It will print : 1 a b c 2 3 4

Pages