Search Placementyogi.com
 

Java Data Types Objective Questions and Answers

  :: Topics ::

1

A _________ is a basic unit of storage in Java.

A.

Identifier

B.

Variable

C.

Constant

D.

Memory

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

A variable is the basic unit of storage in java. All the constants of any of the data types are stored in a variable.

 
2

What is the default data type of a decimal number in Java?

A.

int

B.

float

C.

double

D.

long

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

double is the default data type of a decimal number in Java?

 
3

What would be the output of the following program?

class PrintDefaultValues
{
	public static void main(String[] args) 
	{
		byte b;
		short s;
		int i;
		long l;
		float f;
		double d;
		boolean bl;
		char c;
		System.out.println("byte="+b+" short="+s+" int="+i);
		System.out.println("long="+l+" float="+f+" double="+d);
		System.out.println("char="+c+"boolean="+bl);
	}
}
A.

byte=0 short=0 int=0

long=0 float=0.0 double=0.0

char= boolean=false

B.

byte=0 short=0 int=0

long=0 float=0.000000 double=0.000000

char= boolean=false

C.

Compilation Error

D.

None of the Above

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

We get the following Error:

Error: Variables might not have been initialized.

In Java, Every variable has to be initialized before it can be used.

 
4

What would be the output of the following program?

class PrintDefaultValues
{
	/*Global Variables*/
	byte b;
	short s;
	int i;
	long l;
	float f;
	double d;
	boolean bl;
	char c;
	public static void main(String[] args) 
	{

		System.out.println("byte="+b+" short="+s+" int="+i);
		System.out.println("long="+l+" float="+f+" double="+d);
		System.out.println("char="+c+"boolean="+bl);
	}
}
A.

byte=0 short=0 int=0

long=0 float=0.0 double=0.0

char= boolean=false

B.

byte=0 short=0 int=0

long=0 float=0.000000 double=0.000000

char= boolean=false

C.

Compilation Error

D.

None of the Above

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

Error: non-static variable b cannot be referenced from a static context

There would be multiple Errors like above for all the variables. Here main function is static and static methods are not allowed to access non-static members of a class.

 
5

What would be the output of the following program?

class PrintDefaultValues
{
	static byte b;
	static short s;
	static int i;
	static long l;
	static float f;
	static double d;
	static char c;
	static boolean bl;
	public static void main(String[] args) 
	{
		System.out.println("byte="+b+" short="+s+" int="+i);
		System.out.println("long="+l+" float="+f+" double="+d);
		System.out.println("char="+c+"boolean="+bl);
	}
}
A.

byte=0 short=0 int=0

long=0 float=0.0 double=0.0

char= boolean=false

B.

byte=0 short=0 int=0

long=0 float=0.000000 double=0.000000

char= boolean=false

C.

byte=0 short=0 int=0

long=0 float=0 double=0

char=null boolean=false

D.

Compilation Error

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

It prints the default values of each primitive datatype variables.

Points to Remember:

  1. Static methods can access only static members of a class.
  2. Static members can be accessed by both Static as well as Non-Static Methods.

The main method has access to static members of a class. Also, static members are initialized by the compiler and hence the default values are printed.

 
 

Banner to LogicGuns.com

Hide Page Information

Where can I find Data Types in Java Objective Questions

Placement Yogi contains a lot of objective Questions on Data Types in Java

Practice Java Data Types in JavaMCQ , Multiple Choice Questions

Practice all the questions given here which will help you build concepts on Java Data Types.