Java Basics Flashcards Preview

Java8 > Java Basics > Flashcards

Flashcards in Java Basics Deck (42)
Loading flashcards...
1

Reference types

Reference types refers to objects that they refer to.

A reference points to an object by storing the memory address where teh object is located

2

Can I assign a String refence variable to Integer later?

No, you cannot assign reference variable to different type.

A reference can be assigned to another object of same type.

3

What is important about local variables?

You cannot use local variables without initializing.

It cannot be assigned to default value. It contains garbage value if not assigned any value.

If you try to use it before assigning, compile time error occurs

4

public void findAnswer(boolean check) {
int answer;
int onlyOneBranch;
if (check) {
onlyOneBranch = 1;
answer = 1;
} else {
answer = 2;
}
System.out.println(answer);
System.out.println(onlyOneBranch);
}

Will this compile?

No, it will not compile.

The compiler throws error.

It complains saying that the the field- onlyOneBranch might have not been initialized

5

Which method when overriden, called by Garbage collector?

finalize()

6

How many times a finally could be called by GC?

Zero or one

7

What are the rules for automatic numeric promotion?

If two values have different data types, Java will automatically promote one of the values
to the larger of the two data types.
2. If one of the values is integral and the other is floating-point, Java will automatically
promote the integral value to the floating-point value’s data type.

3. Smaller data types, namely byte, short, and char, are first promoted to int any time
they’re used with a Java binary arithmetic operator, even if neither of the operands is
int.
4. After all promotion has occurred and the operands have the same data type, the resulting
value will have the same data type as its promoted operands

8

int x = 3;
int y = ++x * 5 / x-- + --x;
System.out.println("x is " + x);
System.out.println("y is " + y);

X=2
y=7

9

short x = 10;
short y = 3;
short z = x * y;

What is the actual result?

Compilation error.

We have to downcast it like
short z=(short) (x*y)

10

What is special about compound operator?

Downcasting will be automatically taken care by compound operator

11

5 == 5.00 -> result?

TRUE

12

int a=10;
int b = (a=2);
Syso(a );
syso(b); output?

a=2, b=2

13

int y = 1;
int z = 1;
final int x = y<10 ? y++ : z++;
System.out.println(y+","+z); // Output?

2, 1

14

What should we keep in mind regarding Switch statements?

The value in each case statement must be a compile time contsant. Means it should be final modifier with constant value assigned to it

15

Output?

int x = 0;
for(long y = 0, x = 4; x < 5 && y < 10; x++, y++) {
System.out.print(x + " ");
}

Compilation error- cannot redeclare the variable in for loop initialix=zation block

16

String x="Hello";
String y ="Hello".trim()

x==y?

false.

Because, one is computed at runtime

17

three ways of creating an array

1. int[] abc=new int[3];
2. int[] abc=new int[] {1,2,3};
3. int[] abc={1,2,3}

18

what is the method that prints the elements of the array nicely?

java.util.Arrays.toString()

19

String[] strings = { "stringValue" };
4: Object[] objects = strings;
5: String[] againStrings = (String[]) objects;
6: againStrings[0] = new StringBuilder();
7: objects[0] = new StringBuilder();

line 6 throws compile time error, because StringBuilder cannot fit into String

line 7 throws runime error (ArrayStrore Exception)

20

Method to sort elements in an array?

Arrays.sort()

21

What is the requirement of using BinarySearch in array and what if it doesnt met?

The Array should have been sorted before using BinarySearch.. Otherwise, the result will be unpredictable

22

How to convert to array from List?

list.toArray()

23

what does list.toArray() defaults to?

Array of object

Object[] array = list.toArray();

24

What do we need to do if we want String array from list ?

String[] array = list.toArray(new String[0])

25

What is special about converting from Array to list?

The original array and backed array list are linked

26

Creating List from Array?

Arrays.asList(array)

27

private final ArrayList list=new ArrayList<>();

list.add("String");

Will it compile? If no, why? if Yes, why?

Yes, becasue we can change the state of the object here. Only problem is when we try to assign list to some other object like list=new ArrayList<>(); Now it throws compile time error

28

private static int one;
15: private static final int two;
16: private static final int three = 3;
17: private static final int four;
18: static {
19: one = 1;
20: two = 2;
21: three = 3;
22: two = 4;
23: }

Which of these wont compile?

line 17- > because it is not initialized at 17 or in Static Initialization block

line 21 -> final vaiables cannot be reassignes
line 22 -> final variables cannot be reassigned

29

static imports are meant only for what?

Static imports are meant only for importing static members.
You cant say
import static java.util.Arrays;

instead you can say
import static.java.util.Arrays.*;

30

What are not a valid overloading?

1. Just changing the return type of the method and leaving others unchanged is not the valid overloading
2. Instance and static methods with the same method signatures