Java Building Blocks Flashcards

1
Q

What’s the result of the following code:
String s1 = “java”;
String s2 = “java”;
StringBuilder sb1 = new StringBuilder();
sb1.append(“ja”).append(“va”);
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
System.out.println(s1.toString() == s2);
System.out.println(s1.toString().equals(s2));

A

true is printed out exactly three times

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

What’s an object ?

A

An object is a runtime instance of a class in memory

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

What are the two primary elements of class?

A

Methods(function) & Fields(known as variables). Together these are called members of the class

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

What’s a keyword?

A

They’re a special to the java language definition

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

What’s a method parameter?

A

A method can declare the type of its parameters that must be passed in when called

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

Define method signature?

A

The full declaration of the method(Access method, return type, parameters)

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

How many clases can we declare inside a .java file?

A

You’re allowed to define as many classes as you want within the file and at least one of the clases is allowed to be public and it needs to match the filename

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

Define “public” in the following method signature
public static void main(String[] a){}

A

the keyword is an access modifier. It declares the level of exposure to potential callers in the program

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

Does the following class compile ?
public class $j4va {

}

A

Yes, but it’s not recommended to put the $ at the start of a file as jvm uses it internally

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

Does code compile?
public class 4ngel{}

A

No, class name can’t start with numbers

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

Does code compile?
class lal@{}

A

No, class name cannot contain symbols

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

Does it compile?
import java.util.;
import java.sql.
;

A

No. Type Date is ambiguous

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

Does it compile?
import java.util.Date;
import java.sql.*;

A

Yes, java.util.Date takes precedence over any wildcard.

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

Does it compile?
class Class {
public void Class(){}
}

A

Yes. public void Class() is a method.

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

What’s the output of code:
public class Test{
private int s1;
public static void main(String[] a){
Test t1 = new Test();
System.out.println(t1.s1);
}
}

A

Yes

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

What’s the order of initialization?

A

Fields and instance initializers block are run in the order they appear in file.
The constructor runs after the instance initializer and fields have run

17
Q

What are the two data types java has ?

A

Primitive type and reference type

18
Q

How many java primitive types has?

A

Java has eight built-in data types

19
Q

Does the following declaration works?
long l = 92928282882;

A

No, gives compile error

20
Q

does the following declaration is correct?
float f = 82888282.343;

A

No, value should have a prefix “f”

21
Q

int v1 = 1_2_3_4_789;

A

Yes it compiles

22
Q

What’s a reference type?

A

A reference type refers to an objet (an instance of a class)

23
Q

Where a reference variable stores its value ?

A

A reference value it’s stored in the memory heap. The reference itself does not store anything than the memory address where the object is located

24
Q

Does null can be assigned to a primitive variable?

A

No. Compiler will give you an error

25
Q

Can null be assigned to a referenced type?

A

Yes

26
Q

Does code compile ?
int java, String value;

A

No, multiple variables with different tupes is not allowed

27
Q

Local variables must be _________ before use.

A

Initialized

28
Q

public void valid() {
int y = 10;
int x;
int reply = x * y;
return reply;
}

A

It does not compile. x must be initialized

29
Q

instance vs class variables

A

A class variable has the static keyword before it.
Class variables are shared across multiple objects
Non-local variables are known as instance variables

30
Q

Ordering elements in a class

A

package, import, class