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.

 
 

Crackbill Ad
Hide Page Information

Java Data Types and Variables MCQs with Answers for Written Round Interview

Here you can find Java Data Types and Variables MCQs with answers and explanation. We have a vast collection of MCQs on Data Types and Variables in Java that will help you a lot!

Why learn Java Data Types and Variables?

Data Types and Variables are an integral part of Java and many MCQs in the written round are asked from this topic.In this section you can learn and practice Java Data Types and Variables (MCQs with Answers) to improve your skills in order to face the interview and clear campus and off-campus selection of TCS, Infosys, Wipro etc.

Where can I get Java Data Types and Variables MCQs and Answers with Explanation?

PlacementYogi provides you lots of fully solved Java Data Types and Variables MCQs and answers with explanation. Fully solved examples with detailed answer description, explanation are given and it would be easy to understand.Though we do not have pdf file for Java Data Types and Variables, We have quiz MCQs with answers to help you test yourself..

\n

Where can I get Java Data Types and Variables Interview MCQs and Answers (objective type, multiple choice)?

Here you can find objective type Java Data Types and Variables MCQs and answers for interview and entrance examination.Multiple Answers and true or false type MCQs are also provided.

How to solve Java Data Types and Variables problems?

You can easily solve all kind of MCQs based on Java Data Types and Variables by practicing the exercises (including shortcut Data Types and Variables to solve problems) given below.