ExamVeda
Login
Home
41
Determine output of the following program.
public class Test{
           public static void main(String args[]){
                  System.out.println( Math.floor( Math.random( ) ) ) ;
           }
}
Discuss
Answer & Solution
Answer: Option C
Solution:

random() is a static method defined in Math class.
Syntax: static double random()
random() always return a value in the range 0.0 <= Math.random() <1.0 So, taking floor function of any number between above range always yield 0.0.
Ex: Math.floor(0.958127101241...) = 0.0

    Math.floor(0.05684865565...) = 0.0
42
What is the output of the above program?
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 ) ;
       }     
}
Discuss
Answer & Solution
Answer: Option C
Solution:
The given program creates a class Num that has a constructor with a double parameter.

In the main method, an object of the Num class is created with the value 2, which is passed as a double value.

Since 2 is treated as an integer, Java automatically converts it to a double (2.0), and the constructor prints this value.

Thus, the output of the program is 2.0.
43
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);
	}
}
Discuss
Answer & Solution
Answer: Option A
Solution:
Overloading depends only on the method or constructor name and its parameter list i.e.
         method(int i) and method(String str)

So, overloading does not depend on access modifiers(public, private, protected), access specifiers(static, final) and return type.
44
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)); 
        } 
}
Discuss
Answer & Solution
Answer: Option C
Solution:

Return type of method display is int and we are trying to return string.

45
The implicit return type of a constructor is
Discuss
Answer & Solution
Answer: Option B
Solution:
Constructors are special methods in Java used to create objects of a class.
Unlike other methods, constructors don't have an explicit return type.
They don't use keywords like void, int, etc. to specify what they return.
Instead, their job is to initialize the object being created, setting initial values for its member variables.
When a constructor is called, it automatically returns a newly created object of the class.
Therefore, the implicit return type of a constructor is the class object itself.
So the correct option is B.
46
The finalize() method is called just prior to
Discuss
Answer & Solution
Answer: Option D
No explanation is given for this question. Let's Discuss on Board
47
The main method should be static for the reason
Discuss
Answer & Solution
Answer: Option C
No explanation is given for this question. Let's Discuss on Board
48
Given the following piece of code:
class Person{
        public int number;
}
public class Test{
        public void doIt(int i , Person p){
                i = 5;
                p.number = 8;
        }
        public static void main(String args[]){
                int x = 0;
                Person p = new Person();
                new Test().doIt(x, p);
                System.out.println(x + " " + p.number);
        }
}

What is the result?
Discuss
Answer & Solution
Answer: Option A
No explanation is given for this question. Let's Discuss on Board
49
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.
Discuss
Answer & Solution
Answer: Option B
No explanation is given for this question. Let's Discuss on Board
50
What is the prototype of the default constructor?
public class Test { }
Discuss
Answer & Solution
Answer: Option D
Solution:

Option A and B are wrong because they use the default access modifier and the access modifier for the class is public (remember, the default constructor has the same access modifier as the class).

Option C is wrong. The void makes the compiler think that this is a method specification - in fact if it were a method specification the compiler would spit it out.