Methods and Constructors

Quiz Guidelines


Multiple Choice Question


Which of the following are valid at line 1?

public class X{
//line 1: insert code here.

Select 2 options

A. String s;
B. String s = 'asdf';
C. String s = 'a';
D. String s = this.toString();
E. String s = asdf;

Multiple Choice Question


Consider the following class:

public class Test{
public int id;

Which of the following is the correct way to make the variable 'id' read only for any other class?

Select 1 option

A. Make 'id' private.
B. Make 'id' private and provide a public method getId() which will return its value.
C. Make 'id' static and provide a public static method getId() which will return its value.
D. Make id 'protected'.

Multiple Choice Question


Consider the following class...

class TestClass{
int x;
public static void main(String[] args){
// lot of code.

Select 1 option

A. By declaring x as static, main can access this.x
B. By declaring x as public, main can access this.x
C. By declaring x as protected, main can access this.x
D. main cannot access this.x as it is declared now.
E. By declaring x as private, main can access this.x

Multiple Choice Question



class Triangle
    public int base;
    public int height;
    private static double ANGLE;
    public static double getAngle();
      public static void Main(String[] args)

Identify the correct statements:

Select 1 option
A. It will not compile because it does not implement setAngle method.
B. It will not compile because ANGLE cannot be private.
C. It will not compile because getAngle() has no body.
D. It will not compile because ANGLE field is not initialized.
E. It will not compile because of the name of the method Main instead of main.