Java Language | 20 Minute‐Test 6


Instruction

  • Total number of questions : 20.
  • Time alloted : 20 minutes.
  • Each question carry 1 mark.
  • No Negative marks
  • DO NOT refresh the page.
  • All the best :-).

1.

Which of the following are legal lines of Java code?
1. int w = (int)888.8;
2. byte x = (byte)100L;
3. long y = (byte)100;
4. byte z = (byte)100L;

A.
1 and 2
B.
2 and 3
C.
3 and 4
D.
All statements are correct.

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

Statements (1), (2), (3), and (4) are correct. (1) is correct because when a floating-point number (a double in this case) is cast to an int, it simply loses the digits after the decimal.(2) and (4) are correct because a long can be cast into a byte. If the long is over 127, it loses its most significant (leftmost) bits.(3) actually works, even though a cast is not necessary, because a long can store a byte.

2.

Which of these values can a boolean variable contain?

A.
true & false
B.
0 & 1
C.
Any integer value
D.
true

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

Boolean variable can contain only one of two possible values, true and false.

3.

Literals in java must be appended by which of these?

A.
L
B.
l
C.
D
D.
L and l

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

Data type long literals are appended by an upper or lowercase L.

4.

What is the error in this code?
byte b = 50;
b = b * 50;

A.
b can not contain value 100, limited by its range.
B.
* operator has converted b * 50 into int, which can not be converted to byte without casting.
C.
b can not contain value 50.
D.
No error in this code

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

While evaluating an expression containing int, bytes or shorts , the whole expression is converted to int then evaluated and result is also of type int.

5.

What will this code print?

int arr[] = new int [5];
System.out.print(arr);

A.
0
B.
value stored in arr[0].
C.
00000
D.
Class name@ hashcode in hexadecimal form

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

If we trying to print any reference variable internally, toString() will be called which is implemented to return the String in following form:
classname@hashcode in hexadecimal form

6.

With x = 0, which of the following are legal lines of Java code for changing the value of x to 1?
1. x++;
2. x = x + 1;
3. x += 1;
4. x =+ 1;

A.
1, 2 & 3
B.
1 & 4
C.
1, 2, 3 & 4
D.
3 & 2

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

Operator ++ increases value of variable by 1. x = x + 1 can also be written in shorthand form as x += 1. Also x =+ 1 will set the value of x to 1.

7.

On applying Left shift operator, <<, on an integer bits are lost one they are shifted past which position bit?

A.
1
B.
32
C.
33
D.
31

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

The left shift operator shifts all of the bits in a value to the left specified number of times. For each shift left, the high order bit is shifted out and lost, zero is brought in from right. When a left shift is applied to an integer operand, bits are lost once they are shifted past the bit position 31.

8.

Which of the following operators can operate on a boolean variable?
1. &&
2. ==
3. ?:
4. +=

A.
3 & 2
B.
1 & 4
C.
1, 2 & 4
D.
1, 2 & 3

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

Operator Short circuit AND, &&, equal to, == , ternary if-then-else, ?:, are boolean logical operators. += is an arithmetic operator it can operate only on numeric values.

9.

What is the value stored in x in following lines of code?
int x, y, z;
x = 0;
y = 1;
x = y = z = 8;

A.
0
B.
1
C.
9
D.
8

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

None.

10.

Which of the following loops will execute the body of loop even when condition controlling the loop is initially false?

A.
do-while
B.
while
C.
for
D.
None of the mentioned

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

None.

11.

Which of the following is a valid declaration of an object of class Box?

A.
Box obj = new Box();
B.
Box obj = new Box;
C.
obj = new Box();
D.
new Box obj;

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

None.

12.

Which of the following is a method having same name as that of it’s class?

A.
finalize
B.
delete
C.
class
D.
constructor

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

A constructor is a method that initializes an object immediately upon creation. It has the same name as that of class in which it resides.

13.

Which of the following is a method having same name as that of its class?

A.
finalize
B.
delete
C.
class
D.
constructor

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

A constructor is a method that initializes an object immediately upon creation. It has the same name as that of class in which it resides.

14.

Which of these is correct about passing an argument by call-by-value process?

A.
Copy of argument is made into the formal parameter of the subroutine.
B.
Reference to original argument is passed to formal parameter of the subroutine.
C.
Copy of argument is made into the formal parameter of the subroutine and changes made on parameters of subroutine have effect on original argument.
D.
Reference to original argument is passed to formal parameter of the subroutine and changes made on parameters of subroutine have effect on original argument.

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

When we pass an argument by call-by-value a copy of argument is made into the formal parameter of the subroutine and changes made on parameters of subroutine have no effect on original argument, they remain the same.

15.

Which of these is used as default for a member of a class if no access specifier is used for it?

A.
private
B.
public
C.
public, within its own package
D.
protected

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

When we pass an argument by call-by-value a copy of argument is made into the formal parameter of the subroutine and changes made on parameters of subroutine have no effect on original argument, they remain the same.

16.

Which of these cannot be declared static?

A.
class
B.
object
C.
variable
D.
method

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

static statements are run as soon as class containing then is loaded, prior to any object declaration.

17.

Which of these keywords is used to refer to member of base class from a sub class?

A.
upper
B.
super
C.
this
D.
None of the mentioned

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

Whenever a subclass needs to refer to its immediate superclass, it can do so by use of the keyword super.

18.

A class member declared protected becomes member of subclass of which type?

A.
public member
B.
private member
C.
protected member
D.
static member

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

A class member declared protected becomes private member of subclass.

19.

Which of these keywords can be used to prevent Method overriding?

A.
static
B.
constant
C.
protected
D.
final

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

To disallow a method from being overridden, specify final as a modifier at the start of its declaration. Methods declared as final cannot be overridden.

20.

Which of these method of Object class is used to obtain class of an object at run time?

A.
get()
B.
void getclass()
C.
Class getclass()
D.
None of the mentioned

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

None.

Submit your test now to view the Results and Statistics with answer explanation.