Working with Java Data Types Flashcards

1
Q

Given:

  1. public class Java8ExamPractice{
  2. static Integer i;
  3. public static void main (String [] args) {
  4. Double j = 0.25;
  5. Double z = j + i;
  6. System.out.print(z);
  7. }
  8. }

What is the output?

  1. 0.25
  2. 0.0
  3. An Exception
  4. Compilation fails
A

3 is the answer, A NullPointerException is thrown.

i is a wrapper object with the default value of null thus trying to access it before its been initialised results in an exception

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Given:

  1. public class Java8ExamPractice{
  2. public static void main (String [] args) {
  3. short s = 10;
  4. s += 10;
  5. s++
  6. s = s+ 1;
  7. System.out.print(z);
  8. }
  9. }

What is the output?

A. 21

B. 22

C. Compilation fails on line 4

D. Compilation fails on line 6

E. Compilation fails on multiple lines

A

D. Compilation fails on line 6

Line 6 is attempting to add an int to a short which should result in an int as per integer promotion rules.

However in this case the result is a short thus compilation fails as you need to explicitly cast to fit into a short.

Compilation succeeds on line 4 as increment operators with = implictly casts

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Given:

  1. public class Java8ExamPractice{
  2. public static void main (String [] args) {
  3. Integer a = Integer.decode(“10”);
  4. Integer b = new Integer(“20”);
  5. Integer c = Integer.valueOf(“30”)
  6. System.out.print(a +b +c);
  7. }
  8. }

What is the output?

A. 30

B. 60

C. An exception is thrown

D. Compilation fails on line 4

E. Compilation fails on line 5

A

B. 60

There are multiple ways to create an Integer object:

  1. Using a constructor that takes a string e.g new Integer(“20”)
  2. Calling the decode method which takes a String e.g Integer.decode(“10”)
  3. Using the valueOf method which accepts a String or int literal
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Which of the following is valid?

  1. boolean b = tr_ue;
  2. double d = 0._42;
  3. long l = 1000_L;
  4. int i = _1000;
  5. None of the above
A
  1. None of the above

You can only place _ between digits but NOT:

  1. at the beginiing or end of a number
  2. next to a decimal point e.g line 2
  3. before L or F
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Which of the following are valid primitive literals?

A. 1, ‘c’, “a”

B. 1, 1.5f, True

C. ‘BF’, 10, “Sure”

D. 1.2D, ‘c’, 1f

E. None of the above

A

D. 1.2D, ‘c’, 1f ; double, char & float are primitives

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Which of the following statements will print true when executed? Select 3:

  1. System.out.println(Boolean.parseBoolean(“true”));
  2. System.out.println(new Boolean(null));
  3. System.out.println(new Boolean());
  4. System.out.println(new Boolean(“true”));
  5. System.out.println(new Boolean(“trUE”));
A

1, 4 & 5 are correct.

Explanation:

2 . new Boolean(null) will print false

  1. won’t compile as Boolean doesnt have a no args constructor
  2. new Boolean(“true”) will be parsed as true as long as there are no whitespaces.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
Given the following class, which of the given blocks can be inserted at line 1 without errors? 
public class InitClass{
 private static int loop = 15 ;
 static final int INTERVAL = 10 ;
 boolean flag ;
 //line 1
}

Choose 4:

  1. static {System.out.println(“Static”); }
  2. static { loop = 1; }
  3. static { loop += INTERVAL; }
  4. static { INTERVAL = 10; }
  5. { flag = true; loop = 0; }
A

1, 2,3 and 5 are true.

  1. INTERVAL is final and so it can never be changed after it is given a value.
  2. non-static and static fields can both be accessed from an instance initialiser block.

Note: Instance fields can’t be accessed directly from static blocks or methods. you have to create a new instance first to access them.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
What will be the result of attempting to compile and run the following code?
public class InitClass{
 public static void main(String args[] ){
 InitClass obj = new InitClass(5);
 }
 int m;
 static int i1 = 5;
 static int i2 ;
 int j = 100;
 int x;
 public InitClass(int m){
 System.out.println(i1 + " " + i2 + " " + x + " " + j + " " + m);
 }
 { j = 30; i2 = 40; } // Instance Initializer
 static { i1++; } // Static Initializer
}

Choose 1 option:

  1. The code will fail to compile since the instance initializer tries to assign a value to a static member.
  2. The code will fail to compile since the member variable x will be uninitialized when it is used.
  3. The code will compile without error and will print 6 40 0 30 5 when run.
  4. The code will compile without error and will print 5, 0, 0, 100, 5 when run.
  5. The code will compile without error and will print 5, 40, 0, 30, 0 when run.
A
  1. The code will compile without error and will print 6 40 0 30 5 when run.

The value 5 is passed to the constructor to the local (automatic) variable m. So the instance variable m is shadowed. Before the body of the constructor is executed, the instance initializer is executed and assigns values 30 and 40 to variables j and i2, respectively.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What will be the output when you run this class?

class A1{

static int i = 10;

static { System.out.println(“A1 Loaded “); }

}

public class A{

static { System.out.println(“A Loaded “); }

public static void main(String[] args){

System.out.println(“ A should have been loaded”);

A1 a1 = null;

System.out.println(“ A1 should not have been loaded”); System.out.println(a1.i);

}

}

A

A Loaded

A should have been loaded

A1 should not have been loaded

A1 Loaded

10

A should be loaded first as you are using its main method. Even though you are doing A1 a1 = null; A1 will not be loaded as it is not yet used (so the JVM figures out that it does not need to load it yet.) When you do a1.i, you are using A1, so before you use it, it must be loaded. That’s when A1 is loaded. Finally 10 is printed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What will be the output of the following program?

public class EqualTest{

public static void main(String args[]){

Integer i = new Integer(1) ;

Long m = new Long(1);

if( i.equals(m)) System.out.println(“equal”); // 1.

else System.out.println(“not equal”);

}

}

Choose one:

  1. equal
  2. not equal
  3. Compile time error at //1
  4. Runtime error at //1
  5. None of the above.
A
  1. Not equal
Signature of equals method is : boolean equals(Object o); So it can take any object.
The equals methods of all wrapper classes first check if the two object are of same class or not. If not, they immediately return false. Hence it will print not equal.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

When is the Object created at line //1 eligible for garbage collection?

public class TestClass{

public Object getObject(){

Object obj = new String(“aaaaa”); //1

Object objArr[] = new Object[1]; //2

objArr[0] = obj; //3

obj = null; //4

objArr[0] = null; //5

return obj; //6

}

}

A

Just after line 5

After line 3, both, obj and objArr[0] are pointing to the same String object.
After line 4, obj points to null but objArr[0] is still pointing to the String object.
After line 5, objArr[0] also starts pointing to null so there is no reference left that is pointing to the String object. So it is now available for Garbage collection.

  1. An object can be made eligible for garbage collection by making sure there are no references pointing to that object.
  2. You cannot directly invoke the garbage collector. You can suggest the JVM to perform garbage collection by calling System.gc();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Which of these assignments are valid?

  1. short s = 12 ;
  2. long g = 012 ;
  3. int i = (int) false;
  4. float f = -123;
  5. float d = 0 * 1.5;
A

1, 2,4 are correct

  1. This is valid since 12 can fit into a short and an implicit narrowing conversion can occur.
  2. 012 is a valid octal number.
  3. Values of type boolean cannot be converted to any other types.
  4. Implicit widening conversion will occur in this case.
  5. double cannot be implicitly narrowed to a float even though the value is representable by a float.

Note that
float d = 0 * 1.5f; and float d = 0 * (float)1.5 ; are OK

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What conditions must be met for mplicit narrowing primitive conversion to occur?

A
  1. The expression is a compile time constant expression of type byte, char, short, or int.
  2. The type of the variable is byte, short, or char.
  3. The value of the expression (which is known at compile time, because it is a constant expression) is representable in the type of the variable.

Note that implicit narrowing conversion does not apply to long or double. So, char ch = 30L; will fail even though 30 is representable in char.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are the rules regarding compile time constants?

A

They must be declared final

They are of primitive data types or String

They must be initialized with their declaration.

Their value must be constant expression.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
What will the following program print?
public class TestClass{
 public static void main(String[] args){
 unsigned byte b = 0;
 b--;
 System.out.println(b);
 }
}

Choose an option:

  1. 0
  2. -1
  3. 255
  4. -128
  5. It will not compile.
A
  1. it will not compile

There no unsigned keyword in java! A char can be used as an unsigned integer.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Which of the following statements can be inserted at // 1 to make the code compile without errors?
public class InitTest{
static int si = 10;
int i;
final boolean bool;
// 1
}

  1. instance { bool = true; }
  2. InitTest() { si += 10; }
  3. { si = 5; i = bool ? 1000 : 2000;}
  4. { i = 1000; }
  5. { bool = (si > 5); i = 1000; }
A
  1. { bool = (si > 5); i = 1000; }

A final variable must be initialized when an instance is constructed, or else the code will not compile. This can be done either in an instance initializer or in EVERY constructor.

17
Q

Which of the following are valid code snippets appearing in a method:

Choose 3:

  1. int a = b = c = 100;
  2. int a, b, c; a = b = c = 100;
  3. int a, b, c=100;
  4. int a=100, b, c;
  5. int a= 100 = b = c;
A

2, 3 and 4

Java does not allow chained initialization in declaration so option 1 and 5 are not valid.

18
Q

Consider the following code:

class MyClass { }

public class TestClass{

MyClass getMyClassObject(){

MyClass mc = new MyClass(); //1

return mc; //2

}

public static void main(String[] args) {

TestClass tc = new TestClass(); //3

MyClass x = tc.getMyClassObject(); //4

System.out.println(“got myclass object”); //5

x = new MyClass(); //6

System.out.println(“done”); //7

}

}

After what line the MyClass object created at line 1 will be eligible for garbage collection?

  1. 2
  2. 5
  3. 6
  4. 7
  5. Never, till the program ends
A
  1. 6

At line 6, x starts pointing to a new MyClassObject and no reference to the original MyClass object is left.

19
Q
Given:
public class Square {
 private double side = 0; // LINE 2
 public static void main(String[] args) { // LINE 4
 Square sq = new Square(); // LINE 5
 side = 10; // LINE 6
 }
}
What can be done to make this code compile and run?
  1. replace // LINE 2 with: private int side = 0;
  2. replace // LINE 2 with: public int side = 0;
  3. replace // LINE 5 with: double sq = new Square();
  4. replace // LINE 6 with: sq.side = 10;
A

4.replace // LINE 6 with: sq.side = 10;

side is an instance field in Square class, which means, only objects of Square class will have this field. Therefore, you need to specify which Square object’s side you are trying to access. You are doing that here by using the reference sq that points to an instance of Square class.

Remember that private members of a class are accessible from the same class. The main method is within Square class and that is why you can access the side field of Square class from this method.

An integer can be assigned to a double without a cast but not vice versa.