ch4 Flashcards

1
Q

Spot the invalid methods
~~~
final public void napi() {}
static public void nap1() {}
final static public void nap2() {}
final static void nap3() {}
void public nap4() {}
int public nap5() {}
public int static nap6() {};
public final void walk(){};
~~~

A

nap4, 5 and 6 are invalids

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

What are the elements that are absolutely required when declaring methods?

A

Method declaration -
Return type
Method name
Parameter list
Method body

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

What are the four access modifiers in java?

A

public
private
protected
default

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

what does public access modifier mean?

A

It can be called from any class
protected and classes in the other packages

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

What about private?

A

Only can be called from within the same class.

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

What about the protected class?

A

Can be called from classes in the SAME package or subclass.
protected = inheritance OR SAME package

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

What about default access modifier can be called from?

A

Can be called from the same class within the same package

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

What is default access also known as

A

package private

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

Make a method with default access?

A
void hell(){

}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
default void hello(){}
A

This wont compile bc there is no default keyword

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

Can you name some optional specifiers?

A

static, abstract, final

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

Can you put the optional specifiers in any other?

A

you can specify them in any order

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
public void walk1() {}
public final void walk2() {}
public static final void walk3() {}
public final static void walk4() {}
public modifier void walk5() {} 
public void final walk6() {} 
final public void walk7() {}
A

Invalids are -
walk5()
walk6()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
public String walk4() {return "";}
public String walk2() {return null;}
public int w() {return 9L;}
A

returns empty
returns null
compile error

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

Does the following compile?

public void walk1() { }
public void walk2() { return; }
public String walk3() { return ""; }
public String walk4() { } 
public walk5() { } 
String walk6(int a) { if (a == 4) return ""; }
A

walk4 does not compile bc no return type
walk5 does not compile
walk6 does not compile -> if a is never 4 return wont return

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
public void walk1() { }
public void walk2 { } 
public void walk3(int a) { }
public void walk4(int a; int b) { } 
public void walk5(int a, int b) { }
A

public void walk1() { }
public void walk2 { } // DOES NOT COMPILE
public void walk3(int a) { }
public void walk4(int a; int b) { } // DOES NOT COMPILE
public void walk5(int a, int b) { }

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

Will the following compile?

public void walk1() { }
public void walk2;
A

public void walk1() { }
public void walk2; // DOES NOT COMPILE

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

Where does vargs args parameter be at?

A

last one only

public void hello(int a, int ... num){}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

How many vargs args can you use in a method?

A

Only 1

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

Where can you put vargs args?

A

Only method

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

Can you do this?

public void hello(String … ar)
{
String … anotherArray = ar;
}

A

Only in parameter method is ok
String ..anotherArray is illegal

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

What is the output?

	public static void main(String[] args) {
		runner (new int[] {null,3});
		

	}
	
	public static void runner(int ... i ) {
		System.out.println(i.length);
		
	}
A

DNC null pointer

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

What is the output?

	public static void main(String[] args) {
		runner (new Integer[] {null,3});
		

	}
	
	public static void runner(Integer ... i ) {
		System.out.println(i.length);
		
	}
A

2

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

What is the output?

	public static void main(String[] args) {
		runner (new Integer[] {});
		

	}
	
	public static void runner(Integer ... i ) {
		System.out.println(i.length);
		
	}
	
A

zero

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q
public void walk1(int... nums) { }
public void walk2(int start, int... nums) { }
public void walk3(int... nums, int start) { } 
public void walk4(int... start, int... nums) { }
A

public void walk1(int… nums) { }
public void walk2(int start, int… nums) { }
public void walk3(int… nums, int start) { } // DOES NOT COMPILE
public void walk4(int… start, int… nums) { } // DOES NOT COMPILE

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

If it is private,

Can members in the same class access?

Member in another class in same package?

Member in superclass in diff package

Method/field in non superclass class in diff package

A

Yes
No
No
No

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

If it is public,

Can members in the same class access?

Memeber in another class in same package?

Member in superclass in diff package

Method/field in non superclass class in diff package

A

Yes to all

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

If it is default

Can members in the same class access?

Member in another class in same package?

Member in superclass in diff package

Method/field in non superclass class in diff package

A

yes
yes
no
no

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

If it is protected,

Can members in the same class access?

Member in another class in same package?

Member in superclass in diff package

Method/field in non superclass class in diff package

A

yes
yes
yes
no

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

what is the purpose of static method?

A

For utility or helper methods that don’t require any object state. Since there is no need
to access instance variables, having static methods eliminates the need for the caller to
instantiate the object just to call the method.
■ For state that is shared by all instances of a class, like a counter. All instances must
share the same state. Methods that merely use that state should be static as well.

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

static int count =0;

public static void main (String[] args) {
	Rope a = new Rope();
	a.count = 3;
	Rope b = null;
	a.count = 9;
	System.out.println(count);
	System.out.println(b);
	System.out.println(++b.count);
	
}

}

A

9
null
10

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

What is the output?
~~~

public class Birthday {
static int count = 1;
public static void main(String[] args) {
Birthday a = new Birthday();
a.count=4;
System.out.println(a.counter);
a.counter=69;
System.out.println(a.counter);
Birthday b = null;
System.out.println(b.counter);

}

~~~

A

0
69

69

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

Can you call a instance method or variable from a static method?

A

Absolutely not.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q
public class chapterOne {	
	
    int count = 0;
	public static void main(String[] args) {
		chapterOne a = new chapterOne();
		a.count=4;
		chapterOne b = new chapterOne();
		a.count=5;
		chapterOne cc = null;
		cc.count = 29;
		System.out.println(cc.count);
		
	}
A

Null pointer exception

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

What is the output?

private static final Integer[] values = new Integer[] {3,4,5};
private static final String val = null;
static final StringBuilder change = new StringBuilder("Change");
public static void main(String[] args) {
	values[0]=null;
	change.append("add");
	System.out.println(values[0]);
	System.out.println(val);
	System.out.println(change);
A

null
String val cannot be changed;
StringBuilder is ok

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

You cannot do this
static final Integer change = new Integer(1);
public static void main(String[] args) {
change=3;
System.out.println(change);

}
A

remember this.

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

public class Counter {
private static int count;
public Counter() { count++; }

public static void main(String[] args) {
Counter c1 = new Counter();
Counter c2 = new Counter();
Counter c3 = new Counter();
System.out.println(count);
}
}

A

3y

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

public class ReferenceTypes {
public void fly(String s) {
System.out.print(“string “);
}
public void fly(Object o) {
System.out.print(“object “);
}
public static void main(String[] args) {
ReferenceTypes r = new ReferenceTypes();
r.fly(“string”);
r.fly(true);

A

String
Object
r.fly(true), tries to find boolean if not found it will be Object.
r.fly(4), tries to find int, then Integer, if both not found, it will be Object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
39
Q
public class Rope {
 public void fly(int i) {
 System.out.print("int ");
 }
 public void fly(short i) {
 System.out.print("short ");
 }
 public void fly(long l) {
 System.out.print("long ");
 }
 public void fly(Object o) {
	 System.out.println("obj");
 }
 public static void main(String[] args) {
 Rope p = new Rope();
 p.fly(null);
 p.fly(true);
 p.fly(3.4f);
 p.fly(23);
 } }
A

Obj
Obj
Obj
int

40
Q

What will they print out?

public class Static {
 private String name = "Static class";
 public static void first() { }
 public static void second() { }
 public void third() { System.out.println(name); }
 public static void main(String args[]) {
 first();
 second();
 third(); 
 } }
A

third() makes it not compiles bc third is referencing to nonstatic method.

41
Q

Can you do the following?
Static method ->Calling Another static method or variable
Static method calling another instance method or instance variable
Instance method called a static method or variable
Instance method calling another instance method or variable
Also explain how for each?
Can you demonstrate this idea on pen paper?

public class oca {
		 public static int count;
		 public static void addGorilla() { count++; }
		 public void babyGorilla() { count++; }
		 public void announceBabies() {
		 addGorilla(); 
		 babyGorilla(); 
		  }
		 public static void announceBabiesToEveryone() {
		 addGorilla();
		 babyGorilla(); 
		 }
		 public int total;
		 public static average = total / count; 
		 

}
A

Yes you can using classname
Nope
yes -> using class name Using a reference variable
yes -> Using a reference variable

public class oca {
		 public static int count;
		 public static void addGorilla() { count++; }
		 public void babyGorilla() { count++; }
		 public void announceBabies() {
		 addGorilla(); 
		 babyGorilla(); 
		  }
		 public static void announceBabiesToEveryone() {
		 addGorilla();
		 babyGorilla();  //doesnot
		 }
		 public int total;
		 public static average = total / count; //does not
		 

}
42
Q

What is the output?
private static int one;
private static final int two =9;

static {
	
	one =1;
	one =3;
	two = 10;
}
A

one is fine,
two gives compile error bc you declared it as constant. you cannot declare it twice.

43
Q

what is static initializer?

A

The static initializer is a static {} block of code inside java class, and run only one time before the constructor or main method is called.

is a block of code static { … } inside any java class. and executed by virtual machine when class is called.
No return statements are supported.
No arguments are supported.
No this or super are supported.

44
Q

What is the output?

public class oca {
	{
		System.out.println("Printing non-static block");
	}

	private static int one;
	private static final int two = 0;
	private static final int three;

	public static void main(String... args) {
		System.out.println("Main method");
	}

	static {
		System.out.println("Printing static " + two);
		System.out.println("hack 1st");
	}

	static {

		System.out.println("hack second");
		three = 3;
		System.out.println("Printing static " + three);
	}

}
A

Printing static 0
hack1st
hack second
Printing static 3
Main method

The point is that things in static block execute before the main method.

45
Q

Can you do this? explain?

public class Initializers {
 private static final int NUM_BUCKETS = 45;
 public static void main(String[] args) {
 NUM_BUCKETS = 5; 
 } }
A

no bc you are trying to change the final variable..

46
Q

How about trying this

private static final ArrayList<String> values = new ArrayList<>();
public static void main(String[] args) {
 values.add("changed");
}
A

yes bc you are allowed to call methods on reference variables.

47
Q

import static java.util.Arrays;
import static java.util.Arrays.asList;

A

import static java.util.Arrays; // DOES NOT COMPILE because its not static
2: import static java.util.Arrays.asList; compiles bc it is static

48
Q

Can you do the following?
import static statics.A.TYPE;
import static statics.B.TYPE;

A

No
import static statics.A.TYPE;
import static statics.B.TYPE; //compile error

49
Q

What does it print?

public class what {
	public static void main(String[] args)
	{
		int num =4;
		newNumber(5);
		System.out.println(num);
	}
	
	public static void newNumber(int num) {
		num =8;
	}

}
A

4.

If you got this wrong, look at this again
~~~
public class what {
public static void main(String[] args)
{
int num =4;
newNumber(5);
System.out.println(num);
}

public static void newNumber(int number) {
	number =8;
}

}
~~~

50
Q

What is the output?

public class what {
	
	private int num = 2;
	
	public static void main(String[] args)
	{
		what w = new what();
		w.changeNum(5);
		System.out.println(w.num);
	}
	
	public void changeNum(int num) {
		this.num = num;
	}

}
A

5

51
Q
class Rabbit4 {
 private Rabbit4() { }
}

Rabbit4 r4 = new Rabbit4(); 
A

Rabbit4 made the constructor private so
that other classes could not call it.

52
Q

Can you do this?

public static void main(String[] args) {
 Rope rope = new Rope();
 rope.play();
 Rope rope2 = null;
 rope2.play();
 }
 }
A

yes

53
Q

What is method overloading?

A

Method overloading occurs when
Same method name with diff method signatures/parameters

+ different access modifiers, specifiers (like static), return types, and
exception lists.

54
Q

Can you do this?

void fly(int numMiles, short numFeet) { }
void fly(short numFeet, int numMiles) { }
void fly(short w, int l){}
A

First two yes, last is not bc to java sees the 2nd and 3rd as duplicates

55
Q

Will it compile?

public void fly(int[] lengths) { }
public void fly(int... lengths) { }
A

No. will not compile. Both are arrays.

56
Q

Will it compile?

public static void fly(int numMiles) { System.out.println(numMiles);}
public static void fly(Integer numMiles) {System.out.println(numMiles);}

what happens when primitive int isnt passed?	
A

yes.
only then it will autobox

57
Q

Give the order to load overloaded method

A
  1. Exact match by type
  2. Larger primitive type
  3. Autoboxed type
  4. vargs type
58
Q

What is the use of constructors?

A

To create a new object? its instantiation because it creates a new instance of the class.
Also used to initialize instance variables.

59
Q

Like, what do you mean initialize instance variable with the help of constructor?

A
public class constructorTesting {
	String thisIsAnInstanceVariable;
	int redundantlyInitialize;


public constructorTesting(String thisIsAnInstanceVariable, int someOtherName) {

		this.thisIsAnInstanceVariable = thisIsAnInstanceVariable;
		this.redundantlyInitialize = someOtherName;

	}
}
60
Q

Why is this redundant?

public class constructorTesting {
	String thisIsAnInstanceVariable;
	int redundantlyInitialize;
	public constructorTesting(String thisIsAnInstanceVariable, int someOtherName) {
		this.thisIsAnInstanceVariable = thisIsAnInstanceVariable;
		this.redundantlyInitialize = someOtherName;
	}
}
A

Because you are doing this for variable that’s not duplicate.

61
Q
private String color;
	private int height;
	private int lenght;
	public staticinit(int h, int l) {
		// TODO Auto-generated constructor stub
		h = this.height;
		this.lenght=l;
		color = "white";
	}
	
	public static void main(String[] args) {
		staticinit a = new staticinit(1, 3);
		System.out.println(a.lenght + " "+ a.height + " "+ a.color);
	}
A

length = 3
height = 0
color = white

62
Q

How do you call a constructor from another constructor?

public class constructorTesting {
	String thisIsAnInstanceVariable;
	int redundantlyInitialize;
	String hello;
	String world;
	public constructorTesting(String thisIsAnInstanceVariable, int someOtherName) {
		this.thisIsAnInstanceVariable = thisIsAnInstanceVariable;
		this.redundantlyInitialize = someOtherName;
		System.out.println(this.thisIsAnInstanceVariable);
		System.out.println(this.redundantlyInitialize);
	}
	public constructorTesting(String thisIsAnInstanceVariable, String hello, String world, int someOtherName) {
		this.thisIsAnInstanceVariable = thisIsAnInstanceVariable;
		this.redundantlyInitialize = someOtherName;
		this.hello="hello";
		this.world="world";
		System.out.println(this.thisIsAnInstanceVariable);
		System.out.println(this.redundantlyInitialize);
		System.out.println(this.hello);
		System.out.println(world);
	}
	
	public constructorTesting(String a) {
		this.thisIsAnInstanceVariable=a;
	}
	

}
A
	public constructorTesting(int a) {
		this ("Stone", "MONEY", "120kSalary", a);
	}
63
Q

Can you do the order of initialization?

A
  1. If there is a superclass, initialize it first
  2. Static variable declarations and static initializers in the order they appear in the file.
  3. Instance variable declarations and instance initializers in the order they appear in the file.
  4. The constructor.
64
Q
public class order {
	{
		System.out.println("here");
	}
	
	{
		System.out.println("After here");
	}
	
	static {
		System.out.println("first");
	}
	public order() {
		// TODO Auto-generated constructor stub
		System.out.println("Constructor is last");
	}

	public static void main(String[] args) {
		System.out.println("second");
		order a = new order();
		
	}
}
A

first
second
here
After here
Constructor is last

65
Q
public class order {
	{
		System.out.println("here");
	}
	
	{
		System.out.println("After here");
	}
	
	static {
		System.out.println("??");
	}
	public order() {
		// TODO Auto-generated constructor stub
		System.out.println("Constructor is last");
	}

	public static void main(String[] args) {
		System.out.println("hello");
		
	}
}
A

??
hello

66
Q

public class order {
static {
add(2);
}

static void add(int num) {
	System.out.print(num + " ");
}

order() {
	add(5);
}

static {
	add(4);
}
{
	add(6);
}
static {
	new order();
}
{
	add(8);
}

public static void main(String[] args) {
} }
A

2,4,6,8,5

Pay attention to constructor. Constructor is 5.

67
Q

What is a java bean?

A

JavaBean is just a standard. It is a regular Java class, except it follows certain conventions:

All properties are private (use getters/setters)
A public no-argument constructor

68
Q

Name the standard for java bean

A
private int var;

public boolean isHell(){
return happy
}

public int getNum(){
return num;
}

public setNum(int var){
this.var = var;
}

private property
boolean is
setSomething
getSomething

69
Q

How to make a class immutable?

A

Just use constructors as setters. Do not use setters

70
Q

What is lamda?

A

anon method

71
Q

Can you wap to create a n arraylist and print using lamda

A

ArrayList<Integer> b = new ArrayList<>();
b.add(1);
b.add(2);</Integer>

b.forEach(n -> {System.out.println(n);});

72
Q

What is functional interface?

A

Interface with only 1 abstract method.

Good practise to have @FunctionalInterface

73
Q

define all the rules for overloaded methods

A
  • Changes in the number of parameters that are accepted
  • Changes in tht types of parameters are accepted
  • Changes in the positions of the parameters are accepted
74
Q

Each lamba expression has what mandatory fields?

A

Parameter name
Arrow
Lambda body

75
Q

Can you name all the expressions that can go into lamda

A

Parameter type (optional)
Parameter name
Labda body
arrow
Curly braces (optional)
Keyword return(optional)

76
Q

What is predicate?

A

Functional interface. More specifically generic.

77
Q

Immutable classes ?

A

Immutable allows private instance variables.
Does not have setters. Only can have constructor set.

78
Q

Where can static final variables be set?

A

Only once.
In the declaration line or in the static initialization block

79
Q

Java uses to send data into method?

A

pass by value.

80
Q

What variable is always available to all the instances of the class?

A

static

81
Q

A class may contain more than one no-arg constructor?

A

false

82
Q

Which of the following data types can be modified after they are passed to a method as an argument?
int[]
long
boolean
String

A

only int[]

83
Q

void return type

A

it calls the return command with no values and exit the method.

84
Q

static import
import static Clothes.Store or import static Clothes.Store.getClothes?

A

used with the members of the class.
so method should be included.

85
Q

A final static variable may be set in the constructor

A

No. Remember only in static initializer or when they are first delcared.

However if they are not declared in neither then you can set in contructor.

86
Q

Overloaded methods can have the same return type?
what else can be different? same?

A

True.
only the same name, different parameters

87
Q

Can you do this?
final protected static int a=6;

A

final modifier cannot apprear before access modifier

88
Q

Does the following work? why why not?
~~~

public class Bo {
private static int price=5;
public boolean sell() {
if(price<10) {
price++;
return true;
}else if(price>=10) {
return false;
}
}
public static void main(String[] argss) {

} ~~~
A

No because the return is missing, out of scope.

89
Q

Output?

public class Bo {
	public int price=5;
	public boolean sell() {
		boolean bool=false;
		if(price<10) {
			price++;
			bool= true;
		}else if(price>=10) {
			bool= false;
		}
		return bool;
	}
	public static void main(String[] argss) {
		Bo a =new Bo();
		new Bo().sell();
		new Bo().sell();
		System.out.println(a.price);
	}

}
A

5

90
Q

Output?

public class Bo {
	public int price=5;
	public boolean sell() {
		boolean bool=false;
		if(price<10) {
			price++;
			bool= true;
		}else if(price>=10) {
			bool= false;
		}
		return bool;
	}
	public static void main(String[] argss) {
		new Bo().sell();
		new Bo().sell();
		System.out.println(price);
	}

}
A

DNC

91
Q

output

public class Bo {

	public void nested() {
		nested(2,true);
	}
	public int nested(int level, boolean s) {
		return  nested(level);
	}
	public int nested(int l) {
		return l+1;
	}

	
	public static void main(String[] argss) {
		System.out.println(new Bo().nested());
		
	}

}
A

the overloaded constructor works.
sysout gives problem bc there is nothing to print.
its a runtime exception.

92
Q

Output?

public class Bo {

	private  Object c;
	protected Object getContents() {
		return c;
	}
	protected void setContents(Object c) {
		this.c=c;
	}

	public void showPresent() {
		System.out.println("You gift "+ c);
	}
	
	public static void main(String[] argss) {
		Bo a = new Bo();
		a.setContents(a);
		a.showPresent();
		
		
	}

}
A

You gift Bo@5e265ba4
yes

93
Q

output

public class Bo {

	private final  Object c;
	protected Object getContents() {
		return c;
	}
	protected void setContents(Object c) {
		this.c=c;
	}

	public void showPresent() {
		System.out.println("You gift "+ c);
	}
	
	public static void main(String[] argss) {
		Bo a = new Bo();
		a.setContents(a);
		a.showPresent();
		
		
	}

}
A

you didnt initialize the final vairbale.

94
Q
public class Bo {

	private int size;
	public Bo(int size) {
		this.size=size;
	}
	public static void sendHome(Bo p) {
		p=new Bo(4);
		p.size=33;

	}
	
	public static void main(String ... argss) {
		Bo a = new Bo(5);
		sendHome(a);
		System.out.println(a.size);	
		
	}

}
A

a.size=5.
look the reference variable.
p is none of the business.

95
Q

Which will work.

public class Bo {

	public static void water() {
		
	}
	public void get() {
		Bo.water();
		water();
		this.Bo.water();
		this.water();
	}
	
	

}
A

Bo.water()
water()
this.water()//bad coding

this.Bo.water()//wtf is this.