Java Basics Flashcards

1
Q

Reference types

A

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

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

Can I assign a String refence variable to Integer later?

A

No, you cannot assign reference variable to different type.

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

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

What is important about local variables?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
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?

A

No, it will not compile.

The compiler throws error.

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

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

Which method when overriden, called by Garbage collector?

A

finalize()

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

How many times a finally could be called by GC?

A

Zero or one

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

What are the rules for automatic numeric promotion?

A

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.

  1. 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.
  2. 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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

A

X=2

y=7

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

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

What is the actual result?

A

Compilation error.

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

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

What is special about compound operator?

A

Downcasting will be automatically taken care by compound operator

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

5 == 5.00 -> result?

A

TRUE

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

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

A

a=2, b=2

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

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

A

2, 1

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

What should we keep in mind regarding Switch statements?

A

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

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

Output?

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

A

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

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

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

x==y?

A

false.

Because, one is computed at runtime

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

three ways of creating an array

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

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

A

java.util.Arrays.toString()

19
Q

String[] strings = { “stringValue” };

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

A

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

line 7 throws runime error (ArrayStrore Exception)

20
Q

Method to sort elements in an array?

A

Arrays.sort()

21
Q

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

A

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

22
Q

How to convert to array from List?

A

list.toArray()

23
Q

what does list.toArray() defaults to?

A

Array of object

Object[] array = list.toArray();

24
Q

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

A

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

25
Q

What is special about converting from Array to list?

A

The original array and backed array list are linked

26
Q

Creating List from Array?

A

Arrays.asList(array)

27
Q

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

list.add(“String”);

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

A

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
Q

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?

A

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
Q

static imports are meant only for what?

A

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
Q

What are not a valid overloading?

A
  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
31
Q

public void fly(int[] lengths) { }
public void fly(int… lengths) { }

which method is called if we pass an int[]?

A

Compiler error.

Even though overloading is valid here.

32
Q

what is the difference between calling a method with VarArg parameters and array parameters?

for Eg..,

  1. public void fly(int[] lengths) { }
  2. public void fly(int… lengths) { }

what are the options to call these two methods?

A

Both can be called with array.

eg. .
1. fly(new int[]{1,2,3})

But, only varargs can eb called like below.
fly(1,2,3)

33
Q

which will be called when using fly(3);

public void fly(int numMiles) { }
public void fly(Integer numMiles) { }

A

method with primitive argument.

Java will call the method with most specific value type in the argument

34
Q
public class Glider2 {
public static String glide(String s) {
return "1";
}
public static String glide(String... s) {
return "2";
}
public static String glide(Object o) {
return "3";
}
public static String glide(String s, String t) {
return "4";
}
public static void main(String[] args) {
System.out.print(glide("a"));
System.out.print(glide("a", "b"));
System.out.print(glide("a", "b", "c"));
} }
A

142

35
Q
public class TooManyConversions {
public static void play(Long l) { }
public static void play(Long... l) { }
public static void main(String[] args) {
play(4); //1
play(4L); //2
} }

what happens on 1 and 2?

A
  1. If an int is there, java is happy to convert it to long. means
    int -> long is fine in calling methods.
    but int -> long -> Long => Java will not do, so compilere error as we dont have methods that take int/long/object arguments
  2. It calls the Long version of method
36
Q

what is the keyword to call constructer from another constructor in the same class?
And what is more important in that?

A

this

eg… this (a,b)

And this call must be the first statement inside the costructor from where we call

37
Q

When should be a final variable must have been assigned?

A

By the time constructor completes, all final variables must have been assigned

38
Q

what is functonal programming?

A

Functional progrmming is a way of writing the code more declaratively

39
Q

what are the variables that lambda can access?

A
  1. Static
  2. Instance
  3. Method and local variables - provided there values are not changed
40
Q

(a, b) -> { int a = 0; return 5;} // will this compile?

A

No, lambda cannot change the parameters value passed

41
Q

What is the functional interface that hs a method that returns boolean?

A

Predicate.

public interface Predicate{
public boolean test(T t);
}

42
Q

what is the arrayList method that takes the Predicate?

A

removeIf()