Wednesday 25 July 2012

Object-Oriented concepts - Objective Questions - Part 2

1)

class GFC500 {private String name;}
class GFC501 {
  private String name;
  private void setName(String name) {this.name = name;}
  private String getName() {return name;}
}
class GFC502 {
  private String name;
  public void setName(String name) {this.name = name;}
  public String getName() {return name;}
}

Which class is not tightly encapsulated?

A. GFC501
B. GFC502
C. GFC503
D. None of the above


Answer: D

All three classes are tightly encapsulated, because the data members are private. A tightly encapsulated class can have public accessor and mutator methods, but it is not required to have those methods.

2)

class GFC214 {
  static void m1(boolean b1) {System.out.print("boolean ");}
  static void m1(byte b1) {System.out.print("byte ");}
  static void m1(int i1) {System.out.print("int ");}
  public static void main(String[] args) {
    byte b1; m1(b1 = 1); m1(b1); m1(b1 == 1);
}}

What is the result of attempting to compile and run the program?

A. Prints byte byte byte
B. Prints byte byte boolean
C. Prints boolean byte boolean
D. Compile-time error

Answer: B

The argument of the third method invocation expression, m1(b1 == 1), is the result of the equality expression, b1 == 1. The type of the result and the argument is boolean, so the invoked method is m1(boolean b1).

3)

A class can not be called "tightly encapsulated" unless which of the following is true?

A. All member fields are declared final.
B. The class is not anonymous.
C. The internal data model can be read and modified only through accessor and mutator methods.
D. The class is an inner class.

Answer: C

A class is not tightly encapsulated if the internal data model can be read and/or modified without working through accessor (i.e. get) and mutator (i.e. set) methods.

No comments:

Post a Comment