|
1. Which of the following statements is not true about encapsulation?
A. Tightly encapsulated code is inefficient and can have code redundancy due to unnecessary assessor methods (getter and setter methods).
B. Tightly encapsulated code is easier to maintain.
C. Tightly encapsulated code is robust but it is difficult to do modifications or add newer functionality to the code
D. You need not use inheritance if you are encapsulating your data.
E. Access modifiers are important while enforcing encapsulation.
2. Consider the following class.
1. public class Test {
2. public double var1 = 5;
3. public double var2 = 10;
4.
5. public void setVar1(double var1) {
6. this.var1 = var1;
7. var2 = var1 * 2;
8. }
9.
10. public void setVar2(double var2) {
11. this.var2 = var2;
12. var1 = var2 / 2;
13. }
14.
15. }
Which of the following statements are true about the class Test?
A. The class Test is a tightly encapsulated class.
B. The class Test is not a tightly encapsulated class. However, it will be if we provide two getter methods for var1 and var2.
C. The value of var1 will always be less than value of var2.
D. The value of var2 will always be twice the value of var1.
E. None of the above.
3. A part of problem definition for a Zoo states, “An animal has a care taker and registration number. Elephant is an animal which has weight and a flag indicating whether it has developed a tusk”. You have been asked to implement this system in Java. If you have already defined the Animal class, which of the following would be appropriate member declarations in the Elephant class?
A. private Animal elephant;
B. private int registrationNumber;
C. private float weight;
D. private char withTusk;
E. private String careTaker;
F. private boolean withTusk;
4. The problem definition for Zoo states, “An animal has a care taker and registration number. Elephant is an animal which has weight and a flag indicating whether it has developed a tusk”. You have been asked to implement this system in Java. If you have already defined the Animal class, which of the following is valid opening declaration of the Elephant class that is accessible for all other classes in Zoo application.
A. class Elephant extends Animal {
B. private class Elephant {
C. class Elephant extends Object{
D. public class Animal extends Elephant {
E. public class Elephant extends Animal {
F. class Elephant {
5. Consider the following class.
1. public class Calculator {
2. public float sine(int angle) {
3. // implement calculation
4. }
5. //????
6. }
Which of the following methods can be successfully added (one at a time) at line 5? (Assume that the methods have appropriate implementation)
A. public void sine(int angle) {}
B. public int sine(float angle) {}
C. public float sine(int angle) throws NullPointerException{}
D. private float sine(int angle) {}
E. public float sine() {}
6. Consider the following two classes declared in separate source files.
1. public class Calculator {
2. public float add(float no1, float no2) {}
3. }
1. public class ScientificCalculator extends Calculator {
2. //????
3. }
Which of the following methods can be successfully added (one at a time) on line 2 of class ScientificCalculator? (Assume that the following methods have appropriate implementation)
A. float add(float no1, float no2) {}
B. public float add(float no1, float no2) throws Exception {}
C. protected double add(int no1, double no2) {}
D. private void add(int no1, int no2) {}
E. float add(float no1, int no2) {}
7. Consider the following two classes declared in separate source files.
1. public class Parent {
2. public void printIt(int i) {
3. System.out.println("Value of number is " + i);
4. }
5.
6. public void printIt(float f) {
7. System.out.println("Number is " + f);
8. }
9. }
10.
11.
12. public class Child extends Parent {
13. public void printIt(String s) {
14. System.out.println("Value is " + s);
15. }
16. public void printIt(char c) {
17. System.out.println("Accessing " + c);
18. }
19.
20. public static void main(String[] args) {
21. Child child = new Child();
22. child.printIt(6.8f);
23. child.printIt("" + 9);
24. child.printIt(11);
25. }
26. }
Which of the following will be part of the output when the main method of the Child class is executed?
A. Number is 6.8
B. Value is 9
C. Value of number is 9
D. Accessing 9
E. Number is 11
F. Value of number is 11
8. Consider the following two classes declared in separate source files.
1. public class Base {
2. public Base(int i, int j) {}
3. }
1. public class Derived extends Base {
2. public Derived(int i) {
3. this(i, 0);
4. }
5. public Derived(int i, int j) {
6. super(i, j);
7. }
8. }
Which of the following are valid ways of construction objects of the Derived class?
A. new Derived();
B. new Derived(0,1);
C. new Derived(super);
D. new Derived(0,1,2);
E. new Base().new Derived();
F. new Derived(0);
9. Consider the following classes.
1. public class Tester {
2. public static void main(String[] args) {
3. Calf calf = new Calf("white");
4. }
5. }
6.
7. class Cow {
8. public Cow() {
9. System.out.println("Creating Cow instance");
10. }
11. public Cow(String color) {}
12. }
13.
14. class Calf extends Cow {
15. public Calf(String color) {
16. System.out.println("Creating Calf instance");
17. }
18. }
What is the result after executing class Tester?
A. Creating Calf instance
B. Creating Cow instance
C. Creating Calf instance
D. Creating Cow instance
E. Creating Cow instance
F. Creating Calf instance
G. Compilation fails at line 14, as there is no call to super class constructor.
10. Consider the following classes declared in separate source files.
1. public class MusicalInstrument {
2. public MusicalInstrument(boolean stringInstrument) {}
3. }
1. public class Flute extends MusicalInstrument {
2. // ????
3. }
Which of the following could be the legal constructor for class Flute if added separately on the line 2?
A. public Flute() {}
B. public Flute() { super(); }
C. public Flute() { super(false); }
D. public Flute() { MusicalInstrument(false); }
E. The class Flute need not declare any constructor. Default constructor created by compiler is sufficient.