10. Given the following code, the output is __. Please select "A, B, C, D, or E" as correct option.
class Parent
{
private int x = 5;
int show() { x = 6; return x; }
}
class Child extends Parent
{
public int x = 7;
public Child() { x = 8; x++; x = super.show(); }
}
class Sample
{
public static void main(String[] args)
{
Child c1 = new Child();
System.out.print(c1.x);
}
}
A. 5
B. 6
C. 7
D. 8
E. 9
9. In order to override a method but still use the method in the superclass, what keyword
should be used? Please select "A, B, C, D, or E" as correct option.
A. main
B. void
C. super
D. static
E. protected
8. When overriding a method, where should the new method be defined? Please select "A, B, C, D, or E" as correct option.
A. In the superclass
B. In the main class
C. Outside the main class
D. In the subclass
E. Whichever class the programmer prefers
7. Given the following code, which statement is correct? Please select "A, B, C, D, or E" as correct option.
class Sample
{
int add(int x, int y) { return x+y;} // form 1
String add(String x, String y) { return (x+y)+""; } // form 2
double add(int x, int y, int z) { return x+y+z; } // form 3
void add(int x) { System.out.print(x); } // form 4
}
A. form 1 has a conflict with form 2 because parameters in form 2 are different from
parameters in form 1.
B. form 1 has a conflict with form 3 because the number of parameters in form 3 is not equal
to that of form 1.
C. form 2 is the one that will cause conflicts with others because String is not a primitive data
type.
D. form 4 is the one that will cause conflicts because it is the void type.
E. There is no conflict among these four forms.
6. Given the following code, which form of the "add" method can generate 3.24.1 as output? Please select "A, B, C, D, or E" as correct option.
class Sample
{
int add(int x, int y) { return (x + y); } //form1
float add(float x, float y) { return (x + y); } //form2
double add(double x, double y) { return (x + y); } //form3
char add(char x, char y) {return (char)((int) x + (int) y);} //form4
String add(String x, String y) { return (x + y); } //form5
..........
}
A. form 1
B. form 2
C. form 3
D. form 4
E. form 5
5. Given the following code, which form of the "add" method will be called for execution?
class Sample
{
int add(int x, int y) { return (x + y); } //form1
float add(float x, float y) { return (x + y); } //form2
double add(double x, double y) { return (x + y); } //form3
boolean add(boolean x, boolean y) { return x; } //form4
public static void main(String[] args)
{
Sample s1 = new Sample();
System.out.print(s1.add(3.2, 4.1));
}
}
A. form 1
B. form 2
C. form 3
D. form 4
E. None of the options
4. Which access modifier can prevent methods of a class being overridden? Please select "A, B, C, D, or E" as correct option.
A. public
B. private
C. static
D. protected
E. final
3. The following is an example of __. Please select "A, B, C, D, or E" as correct option.
class Dog
{
int bark() { ... }
}
class Poodle extends Dog
{
double bark() { ... }
}
A. method overloading
B. method overriding
C. method overloading and overriding
D. class overloading
E. class overriding
2. Given the following code, where does the method overloading happen? Please select "A, B, C, D, or E" as correct option.
class Parent
{
public int show() { ... }
public int showIt(int x) { ... }
public int showMe() { ... }
public double showHow() { ... }
public void showAgain() { ... }
}
class Child extends Parent
{
public int show(int x) { ... }
public int showIt(int x) { ... }
public int showMe() { ... }
public int showHow() { ... }
public double showHow() { ... }
}
A. the superclass
B. the Parent class
C. the subclass
D. both the Parent and Child class
E. both the superclass and subclass
1. Given the following code, which method is an example of method overloading? Please select "A, B, C, D, or E" as correct option.
class Parent
{
public int show() { ... }
public int showIt(int x) { ... }
public int showMe() { ... }
public double showHow() { ... }
public void showAgain() { ... }
}
class Child extends Parent
{
public int show(int x) { ... }
public int showIt(int x) { ... }
public int showMe() { ... }
public int showHow() { ... }
public double showHow() { ... }
}
A. show()
B. showIt()
C. showMe()
D. showHow()
E. showAgain()