Java Language | 30 Minute‐Test 9


Instruction

  • Total number of questions : 30.
  • Time alloted : 30 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 [email protected] 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:
[email protected] 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.

21.

Which of these method of class String is used to obtain length of String object?

A.
get()
B.
Sizeof()
C.
lengthof()
D.
length()

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

Method length() of string class is used to get the length of the object which invoked method length().

22.

In below code, what can directly access and change the value of the variable name?

package test;

 class Target

 {

  public String name = "hello";

 }

A.
any class
B.
only the Target class
C.
any class in the test package
D.
any class that extends Target

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

Any class in the test package can access and change name.

23.

Which of these method of class String is used to remove leading and trailing whitespaces?

A.
startsWith()
B.
trim()
C.
Trim()
D.
doTrim()

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

None.

24.

What is the output of this program?

    class overload {

        int x; int y;

        void add(int a){ 

           x =  a + 1; 

       }    

    void add(int a , int b){  

          x =  a + 2;   

     }    

        }      

  class Overload_methods {    

    public static void main(String args[])  

      {      

      overload obj = new overload();    

           int a = 0;    

        obj.add(6, 7);  

          System.out.println(obj.x);   

          } 

 }

A.
6
B.
7
C.
8
D.
9

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

None.

25.

Which of these method of class StringBuffer is used to reverse sequence of characters?

A.
reverse()
B.
reverseall()
C.
Reverse()
D.
reverseAll()

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

reverse() method reverses all characters. It returns the reversed object on which it was called.

26.

Which of these method of class String is used to check weather a given object starts with a particular string literal?

A.
startsWith()
B.
endsWith()
C.
Starts()
D.
ends()

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

Method startsWith() of string class is used to check whether the String in question starts with a specified string. It is specialized form of method regionMatches()

27.

Which of this access specifies can be used for a class so that its members can be accessed by a different class in the same package?

A.
Public
B.
Protected
C.
No Modifier
D.
All of the mentioned

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

Either we can use public, protected or we can name the class without any specifier.

28.

Which of these access specifiers can be used for an interface?

A.
Public
B.
Protected
C.
private
D.
All of the mentioned

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

Access specifier of interface is either public or no specifier. When no access specifier is used then default access specifier is used due to which interface is available only to other members of the package in which it is declared, when declared public it can be used by any code.

29.

Which of these is a super class of wrappers Double & Integer?

A.
Long
B.
Digits
C.
Float
D.
Number

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

Number is an abstract class containing subclasses Double, Float, Byte, Short, Integer and Long.

30.

Which of these is a super class of wrappers Long, Character & Integer?

A.
Long
B.
Digits
C.
Float
D.
Number

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

Number is an abstract class containing subclasses Double, Float, Byte, Short, Integer and Long.

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