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