1. public class A{
2. int add(int i, int j){
3. return i+j;
4. }
5. }
6. public class B extends A{
7. public static void main(String argv[]){
8. short s = 9;
9. System.out.println(add(s,6));
10. }
11.}
A.Compile fail due to error on line no 2
B.Compile fail due to error on line no 9
C.Compile fail due to error on line no 8
D. 15
Correct Option: BExplanation: Compile fail due to error on line no 9
Q: What will be the output?
public class Test{
public static void main(String[] args){
String value = "abc";
changeValue(value);
System.out.println(value);
}
public static void changeValue(String a){
a = "xyz";
}
}
A.abc
B.xyz
C.Compilation fails
D.Compilation clean but no output
Correct Option: AExplanation: abc
Q:
class A{
A(String s){}
A(){}
}
1. class B extends A{
2. B(){}
3. B(String s){
4. super(s);
5. }
6. void test(){
7. // insert code here
8. }
9. }
Which of the below code can be insert at line 7 to make clean compilation ?
A. A a = new B();
B.A a = new B(5);
C.A a = new A(String s);
D.All of the above
Correct Option: AExplanation: A a = new B();
Q: Determine Output:
class A{
public static void method(int i){
System.out.print("Method 1");
}
public static int method(String str){
System.out.print("Method 2");
return 0;
}
}
public class Test{
public static void main(String args[]){
A.method(5);
}
}
A.Method 1
B.Method 2
C.Compile time error as final method can't be overloaded
D.None of the above
Correct Option: AExplanation: Method 1
Q: What is the expected output?
public class Profile {
private Profile(int w) { // line 1
System.out.print(w);
}
public static Profile() { // line 5
System.out.print (10);
}
public static void main(String args[]) {
Profile obj = new Profile(50);
}
}
A.Won't compile because of line (1), constructor can't be private
B. 10 50
C.50
D.Won't compile because of line (5), constructor can't be static
Correct Option: DExplanation: Won't compile because of line (5), constructor can't be static
Q: Which of these is a legal definition of a method named atnyla assuming it throws IOException, and returns void. Also assume that the method does not take any arguments. Select the one correct answer.
public class Profile {
private Profile(int w) { // line 1
System.out.print(w);
}
public final Profile() { // line 5
System.out.print(10);
}
public static void main(String args[]) {
Profile obj = new Profile(50);
}
}
A.Won't compile because of line (1); constructor can't be private
B.Won't compile because of line (5); constructor can't be final
C.50
D.10 50
Correct Option: BExplanation: Won't compile because of line (5); constructor can't be final
Q: What is the output of the program?
class Test{
public int display(int x, int y){
return ("The sum of x and y is " + x + y);
}
public static void main(String args[]){
Test test = new Test();
System.out.println(test.display(4,5));
}
}
A.The sum of x and y is 9
B.The sum of x and y is 45
C.does not compile
D.None of the above
Correct Option: CExplanation: does not compile
Q: The following code contains one compilation error, find it?
public class Test {
Test() { } // line 1
static void Test() { this(); } // line 2
public static void main(String[] args) { // line 3
Test(); // line 4
}
}
A.At line 1, constructor Tester must be marked public like its class
B.At line 2, constructor call
C.At line 3, compilation error, ambiguity problem, compiler can't determine whether a constructor
D.At line 4
Correct Option: BExplanation: At line 2, constructor call
Q: Which of the following statements regarding static methods are correct?
1. Static methods are difficult to maintain, because you can not change their implementation. 2. Static methods can be called using an object reference to an object of the class in which this method is defined. 3. Static methods are always public, because they are defined at class-level. 4. Static methods do not have direct access to non-static methods which are defined inside the same class.