Java Building Blocks Flashcards
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));
true is printed out exactly three times
What’s an object ?
An object is a runtime instance of a class in memory
What are the two primary elements of class?
Methods(function) & Fields(known as variables). Together these are called members of the class
What’s a keyword?
They’re a special to the java language definition
What’s a method parameter?
A method can declare the type of its parameters that must be passed in when called
Define method signature?
The full declaration of the method(Access method, return type, parameters)
How many clases can we declare inside a .java file?
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
Define “public” in the following method signature
public static void main(String[] a){}
the keyword is an access modifier. It declares the level of exposure to potential callers in the program
Does the following class compile ?
public class $j4va {
}
Yes, but it’s not recommended to put the $ at the start of a file as jvm uses it internally
Does code compile?
public class 4ngel{}
No, class name can’t start with numbers
Does code compile?
class lal@{}
No, class name cannot contain symbols
Does it compile?
import java.util.;
import java.sql.;
No. Type Date is ambiguous
Does it compile?
import java.util.Date;
import java.sql.*;
Yes, java.util.Date takes precedence over any wildcard.
Does it compile?
class Class {
public void Class(){}
}
Yes. public void Class() is a method.
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);
}
}
Yes
What’s the order of initialization?
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
What are the two data types java has ?
Primitive type and reference type
How many java primitive types has?
Java has eight built-in data types
Does the following declaration works?
long l = 92928282882;
No, gives compile error
does the following declaration is correct?
float f = 82888282.343;
No, value should have a prefix “f”
int v1 = 1_2_3_4_789;
Yes it compiles
What’s a reference type?
A reference type refers to an objet (an instance of a class)
Where a reference variable stores its value ?
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
Does null can be assigned to a primitive variable?
No. Compiler will give you an error