Working with Java Data Types Flashcards
Given:
- public class Java8ExamPractice{
- static Integer i;
- public static void main (String [] args) {
- Double j = 0.25;
- Double z = j + i;
- System.out.print(z);
- }
- }
What is the output?
- 0.25
- 0.0
- An Exception
- Compilation fails
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
Given:
- public class Java8ExamPractice{
- public static void main (String [] args) {
- short s = 10;
- s += 10;
- s++
- s = s+ 1;
- System.out.print(z);
- }
- }
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
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
Given:
- public class Java8ExamPractice{
- public static void main (String [] args) {
- Integer a = Integer.decode(“10”);
- Integer b = new Integer(“20”);
- Integer c = Integer.valueOf(“30”)
- System.out.print(a +b +c);
- }
- }
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
B. 60
There are multiple ways to create an Integer object:
- Using a constructor that takes a string e.g new Integer(“20”)
- Calling the decode method which takes a String e.g Integer.decode(“10”)
- Using the valueOf method which accepts a String or int literal
Which of the following is valid?
- boolean b = tr_ue;
- double d = 0._42;
- long l = 1000_L;
- int i = _1000;
- None of the above
- None of the above
You can only place _ between digits but NOT:
- at the beginiing or end of a number
- next to a decimal point e.g line 2
- before L or F
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
D. 1.2D, ‘c’, 1f ; double, char & float are primitives
Which of the following statements will print true when executed? Select 3:
- System.out.println(Boolean.parseBoolean(“true”));
- System.out.println(new Boolean(null));
- System.out.println(new Boolean());
- System.out.println(new Boolean(“true”));
- System.out.println(new Boolean(“trUE”));
1, 4 & 5 are correct.
Explanation:
2 . new Boolean(null) will print false
- won’t compile as Boolean doesnt have a no args constructor
- new Boolean(“true”) will be parsed as true as long as there are no whitespaces.
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:
- static {System.out.println(“Static”); }
- static { loop = 1; }
- static { loop += INTERVAL; }
- static { INTERVAL = 10; }
- { flag = true; loop = 0; }
1, 2,3 and 5 are true.
- INTERVAL is final and so it can never be changed after it is given a value.
- 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.
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:
- The code will fail to compile since the instance initializer tries to assign a value to a static member.
- The code will fail to compile since the member variable x will be uninitialized when it is used.
- The code will compile without error and will print 6 40 0 30 5 when run.
- The code will compile without error and will print 5, 0, 0, 100, 5 when run.
- The code will compile without error and will print 5, 40, 0, 30, 0 when run.
- 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.
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 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.
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:
- equal
- not equal
- Compile time error at //1
- Runtime error at //1
- None of the above.
- 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.
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
}
}
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.
- An object can be made eligible for garbage collection by making sure there are no references pointing to that object.
- You cannot directly invoke the garbage collector. You can suggest the JVM to perform garbage collection by calling System.gc();
Which of these assignments are valid?
- short s = 12 ;
- long g = 012 ;
- int i = (int) false;
- float f = -123;
- float d = 0 * 1.5;
1, 2,4 are correct
- This is valid since 12 can fit into a short and an implicit narrowing conversion can occur.
- 012 is a valid octal number.
- Values of type boolean cannot be converted to any other types.
- Implicit widening conversion will occur in this case.
- 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
What conditions must be met for mplicit narrowing primitive conversion to occur?
- The expression is a compile time constant expression of type byte, char, short, or int.
- The type of the variable is byte, short, or char.
- 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.
What are the rules regarding compile time constants?
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.
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:
- 0
- -1
- 255
- -128
- It will not compile.
- it will not compile
There no unsigned keyword in java! A char can be used as an unsigned integer.