Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Data Types

Data Types

1. What is the result of compiling and running the following code?


public class Tester {
static void test(float x) {
System.out.print("float");
}
static void test(double x) {
System.out.print("double");
}
public static void main(String[] args) {
test(99.9);
}
}

No. Answers Correct


1 float
2 double
3 Compilation error
4 Exception is thrown at run time

Explanation:
floating-point numbers are by default of type double.
99.9 is a double not a float
to print “float” cast 99.9 to (float)

2. Which of the followings are correct when tried to compile and execute the below code ?
(choose 2)
class s
{
public static void main(String agr[])
{
short s1=4; //LINE 1
short s2 = s1+=s1; //LINE 2
short s3= s1+s2; //LINE 3
byte b1=(byte)s1 +(byte)s2; //LINE 4
byte b2=(byte)((byte)s1 +(byte)(byte)s2); //LINE 5
}
}

No. Answers Correct


1 compile time error at LINE 1
2 compile time error at LINE 2
3 compile time error at LINE 3
4 compile time error at LINE 4
5 compile time error at LINE 5
6 compiles successfully.

TalentASprint | © Copyright 2012 1


Data Types

Explanation:
Every primitives( byte,short,int,long) in an expression is first converted into int then the
expression is evaluated now if we are assigning the result of the expression to narrower
primitives(like byte or short) without explicitly typecasting it then it will cause compiler
error.
LINE 3: Type mismatch: cannot convert from int to short
LINE 4: Type mismatch: cannot convert from int to byte

3. What is the result of compiling and running the following code?


public class Tester {
static void test(float x) {
System.out.print("float");
}
static void test(double x) {
System.out.print("double");
}
public static void main(String[] args) {
test((float) 99.9);
}
}

No. Answers Correct


1 Float
2 Double
3 Compilation error
4 Exception is thrown at run time

Explanation:
floating-point numbers are by default of type double.
99.9 is a double not a float
to print “float” cast 99.9 to (float)

4. What is the correct output?


class Test {
public static void main(String[] args) {
float f = 1; // line 1
System.out.println(++f); // line 2
}
}

No. Answers Correct


1 compilation error in line 1
2 compilation error in line 2
3 Runtime exception
4 1
5 2.0
6 1.0

TalentASprint | © Copyright 2012 2


Data Types

Explanation:
line 1: int to float, simple widening
line 2: 1.0 + 1 = 2.0

5. What is the result of compiling and running the following code?


public class Tester {
static void test(float x) {
System.out.print("float");
}
public static void main(String[] args) {
test(99.9);
}
}

No. Answers Correct


1 float
2 Compilation fails
3 An Exception is thrown at run time

Explanation:
floating-point numbers are by default of type double.
99.9 is a double not a float
The compiler will complain because it cannot find a method named test() that can
accept a double.

6. if byte b=50;
b=b*2;

No. Answers Correct


1 error ! can't assign an int to a byte
2 b=100
3 b=50

TalentASprint | © Copyright 2012 3

You might also like