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: The variables declared in a class for the use of all methods of the class are called
A.reference variables
B. objects
C. instance variables
D.None of these
Correct Option: CExplanation: instance variables
Q: Determine output of the following program.
public class Test{
public static void main(String args[]){
System.out.println( Math.floor( Math.random( ) ) ) ;
}
}
A.1.0
B. 10.0
C. 0.0
D.0.5
Correct Option: CExplanation: 0.0
Q: What is the output for the below code?
public class Test{
public static void printValue(int i, int j, int k){
System.out.println("int");
}
public static void printValue(byte...b){
System.out.println("long");
}
public static void main(String... args){
byte b = 9;
printValue(b,b,b);
}
}
A.long
B.int
C.Compilation fails
D.Compilation clean but throws RuntimeException
Correct Option: BExplanation: int
Q:
class MyClass{
MyClass(){
System.out.print("one");
}
public void myMethod(){
this();
System.out.print("two");
}
}
public class TestClass{
public static void main(String args[]){
MyClass obj = new MyClass();
obj.myMethod();
}
}
which one is correct
A.two one one
B.one one two
C.one Exception
D.Compilation Error
Correct Option: DExplanation: Compilation Error
Q: Which of the following options is the best for generating random integer 0 or 1?
class Num {
Num(double x ){
System.out.println( x ) ;
}
}
public class Test extends Num {
public static void main(String[] args){
Num num = new Num( 2 ) ;
}
}
A.0
B.2.0
C.Compile time error
D.None of the above
Correct Option: CExplanation: Compile time error
Q: Which of the modifier can't be used for constructors?
A. public
B.private
C.static
D.protected
Correct Option: CExplanation: static
Q: What is the expected output?
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:
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 ?