3.11. Sample exam questions

Q3-1.

Which option defines a well-encapsulated class?


  1. class Template {
        public String font;
    }

  2. class Template2 {
        public String font;
        public void setFont(String font) {
            this.font = font;
        }
        public String getFont() {
            return font;
        }
    }

  3. class Template3 {
        private String font;
        public String author;
        public void setFont(String font) {
            this.font = font;
        }
        public String getFont() {
            return font;
        }
        public void setAuthor(String author) {
            this.author = author;
        }
        public String getAuthor() {
            return author;
        }
    }
  4. None of the above

Q3-2.

Examine the following code and select the correct option(s):

public class Person {
    public int height;
    public void setHeight(int newHeight) {
        if (newHeight <= 300)
            height = newHeight;
    }
}

  1. The height of a Person can never be set to more than 300.
  2. The preceding code is an example of a well-encapsulated class.
  3. The class would be better encapsulated if the height validation weren’t set to 300.
  4. Even though the class isn’t well encapsulated, it can be inherited by other classes.

Q3-3.

Which of the following methods correctly accepts three integers as method arguments and returns their sum as a floating-point number?


  1. public void addNumbers(byte arg1, int arg2, int arg3) {
        double sum = arg1 + arg2 + arg3;
    }

  2. public double subtractNumbers(byte arg1, int arg2, int arg3) {
        double sum = arg1 + arg2 + arg3;
        return sum;
    }

  3. public double numbers(long arg1, byte arg2, double arg3) {
        return arg1 + arg2 + arg3;
    }

  4. public float wakaWakaAfrica(long a1, long a2, short a977) {
        double sum = a1 + a2 + a977;
        return (float)sum;
    }

Q3-4.

Which of the following statements are true?

  1. If the return type of a method is int, the method can return a value of type byte.
  2. A method may or may not return a value.
  3. If the return type of a method is void, it can define a return statement without a value, as follows:

    return;
  4. A method may or may not accept any method arguments.
  5. A method must accept at least one method argument or define its return type.
  6. A method whose return type is String can’t return null.

Q3-5.

Given the following definition of class Person,

class Person {
    public String name;
    public int height;
}

what is the output of the following code?

class EJavaGuruPassObjects1 {
    public static void main(String args[]) {
        Person p = new Person();
        p.name = "EJava";
        anotherMethod(p);
        System.out.println(p.name);
        someMethod(p);
        System.out.println(p.name);
    }

    static void someMethod(Person p) {
        p.name = "someMethod";
        System.out.println(p.name);
    }
    static void anotherMethod(Person p) {
        p = new Person();
        p.name = "anotherMethod";
        System.out.println(p.name);
    }
}


  1. anotherMethod
    anotherMethod
    someMethod
    someMethod

  2. anotherMethod
    EJava
    someMethod
    someMethod

  3. anotherMethod
    EJava
    someMethod
    EJava
  4. Compilation error

Q3-6.

What is the output of the following code?

class EJavaGuruPassPrim {
    public static void main(String args[]) {
        int ejg = 10;
        anotherMethod(ejg);
        System.out.println(ejg);
        someMethod(ejg);
        System.out.println(ejg);
    }
    static void someMethod(int val) {
        ++val;
        System.out.println(val);
    }
    static void anotherMethod(int val) {
        val = 20;
        System.out.println(val);
    }
}


  1. 20
    10
    11
    11

  2. 20
    20
    11
    10

  3. 20
    10
    11
    10
  4. Compilation error

Q3-7.

Given the following signature of method eJava, choose the options that correctly overload this method:

public String eJava(int age, String name, double duration)

  1. private String eJava(int val, String firstName, double dur)
  2. public void eJava(int val1, String val2, double val3)
  3. String eJava(String name, int age, double duration)
  4. float eJava(double name, String age, byte duration)
  5. ArrayList<String> eJava()
  6. char[] eJava(double numbers)
  7. String eJava()

Q3-8.

Given the following code,

class Course {
    void enroll(long duration) {
        System.out.println("long");
    }
    void enroll(int duration) {
        System.out.println("int");
    }
    void enroll(String s) {
        System.out.println("String");
    }
    void enroll(Object o) {
        System.out.println("Object");
    }
}

what is the output of the following code?

class EJavaGuru {
    public static void main(String args[]) {
        Course course = new Course();
        char c = 10;
        course.enroll(c);
        course.enroll("Object");
    }
}

  1. Compilation error
  2. Runtime exception

  3. int
    String

  4. long
    Object

Q3-9.

Examine the following code and select the correct options:

class EJava {
    public EJava() {
        this(7);
        System.out.println("public");
    }
    private EJava(int val) {
        this("Sunday");
        System.out.println("private");
    }
    protected EJava(String val) {
        System.out.println("protected");
    }
}
class TestEJava {
    public static void main(String[] args) {
        EJava eJava = new EJava();
    }
}

  1. The class EJava defines three overloaded constructors.
  2. The class EJava defines two overloaded constructors. The private constructor isn’t counted as an overloaded constructor.
  3. Constructors with different access modifiers can’t call each other.
  4. The code prints the following:

    protected
    private
    public
  5. The code prints the following:

    public
    private
    protected

Q3-10.

Select the incorrect options:

  1. If a user defines a private constructor for a public class, Java creates a public default constructor for the class.
  2. A class that gets a default constructor doesn’t have overloaded constructors.
  3. A user can overload the default constructor of a class.
  4. The following class is eligible for a default constructor:

    class EJava {}
  5. The following class is also eligible for a default constructor:

    class EJava {
            void EJava() {}
    }

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.34.226