Practice examples Flashcards

1
Q
What happens when this is run? Explain.
public class Chicken {
private void layEggs(int...eggs){
	System.out.println("many ");
}

private void layEggs(int eggs){
	System.out.println("one " + eggs + " ");
}

public static void main(String[] args) {

	Chicken c = new Chicken();

	c. layEggs(1,2);
	c. layEggs(3);
	c. layEggs(null);

}

}

A
Runs fine. 
Prints:
 many
 one  3 
 many

This is ok because null can be used where any object is the parameter. The method doesn’t do anything with null in the body so it doesn’t throw a null pointer.

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

What happens when this is run? Explain.

public class Chicken {

private void layEggs(int...eggs){
	System.out.println("many " + eggs[0] + " ");
	System.out.println("many ");
}

private void layEggs(int eggs){
	System.out.println("one " + eggs + " ");
}

public static void main(String[] args) {

	Chicken c = new Chicken();

	c. layEggs(1,2);
	c. layEggs(3);
	c. layEggs(null);

}

}

A

c. layEggs(1,2); // prints many 1
c. layEggs(3); // prints one 3
c. layEggs(null); //throws null pointer. It’s ok to have null in the parameter if any object is required but it throws null pointer when the method body tries to do something with it. System.out.println(“many “ + eggs[0] + “ “);

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

Make an array then pass it to a list.

A

x

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