Test 2 Prep 2 Questions Flashcards

1
Q

All Java classes have a default constructor

A

False

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

The following instantiates an array of ints:

int [ ] array;

A

False

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

The following declares an array of doubles:

double [ ] array;

A

True

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

In the following code, num is a reference type:

int num = 3;

A

False

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

In the following code, array is a reference type:

int [ ] array;

A

True

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

In the following code, array is a reference type:

Integer [ ] array;

A

True

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

In the following code, name is a primitive type:

String name = “marc”;

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
Will the following compile:
ArrayList array = new ArrayList(10);
A

True

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

The following code will compile:

Integer num = 4;

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
What (true or false) will the following method return when run:
public boolean method() {
    String str1 = new String("OOP");
    String str2 = new String("OOP");
    return str1 == str2;
}
A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
The following will compile: 
ArrayList array = new List();
A

False

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

The following will compile:
ArrayList list = new ArrayList();
list.add(“Marc was here”);
System.out.println(list.get(0).toUpperCase());

A

False

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

Given the following code:
public class Chef {
private String name;
private int noodles;

    public Chef(String name, int noodles) {
        this.name = name;
        this.noodles = noodles;
    }
    public Chef(String name) {
        this.name = name;
        this(name, 20);
    }
    public String toString() {
        return this.name + " : "  + this.noodles;
    }
}
... in another file ...
public static void main(String[] args) {
    Chef marc = new Chef("Marc", 50);
    System.out.println(marc);
}

What will happen when the code is run?

a. ) “Chef@[Some hexadecimal numbers]” will be printed
b. ) “Marc : 50” will be printed
c. ) Some runtime error
d. ) Some compile-time error

A

d.)

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

Given some classes and interfaces with following partial declarations:

public abstract class Animal;
public class Snake extends Animal;
public interface Mammal;
public Whale implements Mammal;
public class Tiger extends Animal;

Which of the following (zero or more) will cause a compilation error (assuming all constructors exist):

a. ) Animal steve = new Snake();
b. ) Animal unknownSpecies = new Mammal();
c. ) Mammal sean = new Whale();
d. ) Mammal mike = new Tiger();
e. ) Animal jim = new Animal();

A

b.), d.), and e.)

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

Given the following code:

ArrayList list = new ArrayList(10);

list. add(“Marc”);
list. add(“Chris”);
list. add(Harsh”);
list. remove(“Chris”);

What will list.length return?

a. ) 10
b. ) 3
c. ) 2
d. ) None of the above / other answer

A

d.)

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

What is a method signature?

A

A method’s signature is comprised of the method’s name and the method’s input parameters.

17
Q

In your own words, explain and give an example of overloading

A

The Java programming language supports overloading methods, and Java can distinguish between methods with different method signatures. This means that methods within a class can have the same name if they have different parameter lists.

ex)
cook (int rice);
cook (int rice, int beans);
cook (int rice, int beans, int lettuce);