ExamVeda
Login
Home
31
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);
         }
}
Discuss
Answer & Solution
Answer: Option D
Solution:

Only public, protected, private and default (no access modifier) are legal while declaring constructors.

32
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
	}
}
Discuss
Answer & Solution
Answer: Option B
Solution:

a constructor call (super() or this() ) must be the first statement inside a constructor.
No constructor calls are allowed inside a method.
static void Tester() is a method not a constructor.

33
What will be the return type of a method that not returns any value?
Discuss
Answer & Solution
Answer: Option A
No explanation is given for this question. Let's Discuss on Board
34
Which of the following options is the best for generating random integer 0 or 1?
Discuss
Answer & Solution
Answer: Option C
Solution:
The correct answer is Option C: (int)(Math.random() + 0.5)

In Java, to generate a random integer that can be either 0 or 1, you can use the `Math.random()` method, which returns a random double value between 0.0 (inclusive) and 1.0 (exclusive). To convert this into either 0 or 1, you need to add 0.5 to the result and then cast it to an integer using `(int)`.

Here's the breakdown of the options:

- Option A: `(int)Math.random()` would generate either 0 or a random positive integer, but not necessarily 1.

- Option B: `(int)Math.random() + 1` would generate random positive integers starting from 1 and not 0.

- Option C: `(int)(Math.random() + 0.5)` adds 0.5 to the random value, making it either 0.5 or 1.5, and then casts it to an integer, resulting in either 0 or 1, which is what you want.

- Option D: `(int)(Math.random() + 0.2)` would not reliably generate only 0 or 1; it can produce a range of integers starting from 0.
35
What is Math.floor(3.6)?
Discuss
Answer & Solution
Answer: Option A
Solution:

The method floor gives the largest integer that is less than or equal to the argument.

36
In which area of memory, the system stores parameters and local variables whenever a method is invoked?
Discuss
Answer & Solution
Answer: Option C
No explanation is given for this question. Let's Discuss on Board
37
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);
	}
}
Discuss
Answer & Solution
Answer: Option B
Solution:

Only public, protected, private and default(no modifier) are legal when declaring constructors.

38
What is the expected output?
class Animal {
	Animal() {
		System.out.println("Animal");
	}
}
class Wild extends Animal{
	Wild() {
		System.out.println("Wild");
		super();
	}
}
public class Test {
	public static void main(String args[]) {
		Wild wild = new Wild();
	}
}
Discuss
Answer & Solution
Answer: Option D
Solution:

super() call must be the first statement in a constructor.

39
Which of the modifier can't be used for constructors?
Discuss
Answer & Solution
Answer: Option C
No explanation is given for this question. Let's Discuss on Board
40
The variables declared in a class for the use of all methods of the class are called
Discuss
Answer & Solution
Answer: Option C
No explanation is given for this question. Let's Discuss on Board