Search Placementyogi.com
 

Java Data Types and Variables MCQs with Answers

  :: Topics ::

16

What would be the output of the following program?

class HexPrgm 
{
	public static void main(String[] args) 
	{
		int i=0x10;
		System.out.println(i);
	}
}
A.

10

B.

a

C.

16

See Answer & Explanation Lets Discuss
Correct answer is : C
Explanation

The decimal equivalent of 0x10 is 16.

 
17

What would be the output of the following program?

class HexPrgm
{
	public static void main(String[] args) 
	{
		int i=0xF;
		System.out.println(++i);
	}
}
A.

0

B.

16

C.

17

D.

Compilation Error

See Answer & Explanation Lets Discuss
Correct answer is : B
Explanation

This works fine as the decimal value of hexadecimal 0xF i.e. 15 is assigned to i, and when we increment i , 16 is printed.

 
18

What would be the output of the following program?

class Scope1
{
	public static void main(String args[])
	{
		int i=10;
		System.out.println(i);
		if (i==10)
		{
			int i=20;
			System.out.println(i);
		}
		System.out.println(i);
	}
}
A.

10 20 10

B.

10 20 20

C.

10 10 10

D.

Compilation Error

See Answer & Explanation Lets Discuss
Correct answer is : D
Explanation

Be careful! In Java you can not define the same variabe twice. The compiler throws the following Error:

C:\java_practice\src\Scope1.java:9: error: variable i is already defined in met

hod main(String[])

                        int i=20;

                            ^

1 error

 
19

What would be the output of the following program?

 

class Scope2 
{
	public static void main(String args[])
	{
		int i=10;
		System.out.println("i="+i);
		if (i==10)
		{
			int j=20;
			i=20;
			System.out.println("i="+i+" , j="+ j);
		}
		System.out.println("i="+i+" , j="+ j);
	}
}
A.

i=10

i=20, j=20

B.

i=10

i=10,j=20

C.

Compilation Error

D.

None of the Above

See Answer & Explanation Lets Discuss
Correct answer is : C
Explanation

The variable j is local to the "if" block and hence is not visible outside it.

Hence when we try to use it later while printing, the compiler reports that it is unable to find the symbol j.

C:\java_practice\src\Scope2.java:12: error: cannot find symbol

                System.out.println("i="+i+" , j="+ j);

                                                   ^

  symbol:   variable j

  location: class Scope2

 
20

What would be the output of the following program?

class Scope3 
{
	public static void main(String args[])
	{
		for (int i=1;i<=3 ;i++ )
		{
			int y=1;
			System.out.println("i="+i+" y="+y);
			y++;
		}
	}
}
A.

i=1 y=1

i=2 y=2

i=3 y=3

B.

i=1 y=1

i=2 y=1

i=3 y=1

C.

i=1 y=1

i=1 y=1

i=1 y=1

D.

Compilation Error

See Answer & Explanation Lets Discuss
Correct answer is : B
Explanation

The declaration of variable y is inside the for loop and is executed every time the control enters the loop thus, re-initializing it to 1 everytime.

So, Y remains 1 across all the 3 iterations.

A variable declared inside a block is lost every time the control goes out of the scope thus the y declared here is created afresh and discarded at the end of every iteration.

 
 


Hide Page Information