Search Placementyogi.com
 

Java Data Types Difficult Objective Questions and Answers

  :: Topics ::

1

What would be the output of the following program?

class CharRange 
{
	public static void main(String[] args) 
	{
		char ch=127;
		ch++;
		System.out.println(ch+" = " +(int)ch);
	}
}
A.

? = 128

B.

GarbageCharacter = -128

C.

Compiler Error

D.

None of the Above

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

The program works absolutely fine and prints ? = 128 as the unicode valu of Question mark symbol(?) is 128.

Java uses Unicode to represent characters, Hence the width of a character variable is 16 bits, unlike 8 bits in C. 

The Range of a character variable is 0 to 65536 in java. Hence, ch can have 128 value. 

A char type variable can not have negative values.

 
2

What would be the output of the following program ?

 

class HexChar 
{
	public static void main(String[] args) 
	{
		char c= '\u0061';
		System.out.println(c);
	}
}
A.

'\u0061'

B.

a

C.

97

D.

Compilation Error

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

char c='\u0061' is the way to enter the value of a character in hexadecimal.

'\u' is the excape sequence to tell the compiler that the next 4 digits represent a hexadecimal number.

The Hexadecimal number is converted in to decimal and then the character equivalent of that unicode  number is printed.

0x0061 in decimal means 97 in decimal and the unicode representation of 97 is character 'a' 

 
3

What would be the output of the following program

class OctalChar 
{
	public static void main(String[] args) 
	{
		char c= '\141';
		System.out.println(c);
	}
}
A.

'\141'

B.

a

C.

97

D.

Compilation Error

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

char c='\141' is the way to enter the value of a character in octal.

'\' is the excape sequence to tell the compiler that the following digits represent a octal number.

The octal number is converted in to decimal and then the character equivalent of that unicode  number is printed.

141 in decimal means 97 in decimal and the unicode representation of 97 is character 'a' ,Hence 'a' is printed.

 
4

Which of the following lines perform Automatic Type Conversion? (Multiple Choice)

class AutoConv1 
{
	public static void main(String args[])
	{
		byte b=10;  //line5
		short s=20; //line6
		int i;
		i=b;        //line8
		System.out.println("i="+i);
		i=s;        //line10
		System.out.println("i="+i);
	}
}
A.

line5

B.

line6

C.

line8

D.

line10

See Answer & Explanation Lets Discuss
Correct answer is : A,B,C,D
Explanation

Java performs an automatic type conversion when storing a literal integer constant into variables of type byte, short, long and char.

Thus, All the options are correct.

In the line5 and line6 the integer constant 10 and 20 are assigned to byte and short respectively. Here Automatic Type Conversion occurs.

 

In the line8 and line10 "Widening" happens i.e. byte and short are widened/ typecasted into a larger datatype i.e. int.

 
5

What is the output of the following program?

class MyClass 
{
	public static void main(String[] args) 
	{
		char c="A";
		System.out.println("c="+c+1);
	}
}
A.

66

B.

98

C.

Compilation Error

D.

None of the Above

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

The declaration is incorrect. double quotes around "A" makes it a string and when we try to assign it to C the compiler throws the following error :

C:\java_practice\src\classes\MyClass.java:5: error: incompatible types

                char c="A";

                       ^

  required: char

  found:    String

1 error

 
 

Banner to LogicGuns.com

Hide Page Information

Java Data Types Difficult Objective Questions and Answers

These Java Data Types Difficult Objective Questions and Answers are very useful and will help you to check your Understanding of Java Data Types.
These are a little tough that previous section and hence are mainly for Computer Science Students and Experienced Professionals