Methods and Constructors

Quiz Guidelines

 

Multiple Choice Question

QID: 
292

Which of the following code fragments are valid method declarations?

Select 1 option

A. void method1{ }
B. void method2( ) { }
C. void method3(void){ }
D. method4{ }
E. method5(void){ }

Multiple Choice Question

QID: 
291

What should be the return type of the following method?

public RETURNTYPE methodX( byte by){
double d = 10.0;
return (long) by/d*3;
}

Select 1 option

A. int
B. long
C. double
D. float
E. byte

Multiple Choice Question

QID: 
290

Consider the following code appearing in the same file:

class Data {
    int x = 0, y = 0;
    public Data(int x, int y){
        this.x = x; this.y = y;
    }
}
public class TestClass {
    public static void main(String[] args) throws Exception {
        Data d = new Data(1, 1);
        //add code here
    }
}

Which of the following options when applied individually will change the Data object currently referred to by the variable d to contain 2, 2 as values for its data fields?

Select 2 options

A. Add the following two statements:
d.x = 2;
d.y = 2;
B. Add the following statement:
d = new Data(2, 2);
C. Add the following two statements:
d.x += 1;
d.y += 1;
D. Add the following statement:
d = d + 1;

Multiple Choice Question

QID: 
289

Given:

class Triangle{
public int base;
public int height;
private final double ANGLE;
public void setAngle(double a){ ANGLE = a; }
public static void main(String[] args) {
Triangle t = new Triangle();
t.setAngle(90);
}
}

Select 1 option

A. the value of ANGLE will not be set to 90 by the setAngle method.
B. An exception will be thrown at run time.
C. The code will work as expected setting the value of ANGLE to 90.
D. The code will not compile.

Multiple Choice Question

QID: 
115

What will be the result of attempting to compile and run the following program?

public class TestClass{
public static void main(String args[ ] ){
Object a, b, c ;
a = new String("A");
b = new String("B");
c = a;
a = b;
System.out.println(""+c);
}
}

Select 1 option

A. The program will print java.lang.String@XXX, where XXX is the memory location of the object a.
B. The program will print A
C. The program will print B
D. The program will not compile as a,b and c are of type Object.
E. The program will print java.lang.String@XXX, where XXX is the hash code of the object a.

Pages