Test 2 Prep 2 Questions Flashcards
All Java classes have a default constructor
False
The following instantiates an array of ints:
int [ ] array;
False
The following declares an array of doubles:
double [ ] array;
True
In the following code, num is a reference type:
int num = 3;
False
In the following code, array is a reference type:
int [ ] array;
True
In the following code, array is a reference type:
Integer [ ] array;
True
In the following code, name is a primitive type:
String name = “marc”;
False
Will the following compile: ArrayList array = new ArrayList(10);
True
The following code will compile:
Integer num = 4;
True
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; }
False
The following will compile: ArrayList array = new List();
False
The following will compile:
ArrayList list = new ArrayList();
list.add(“Marc was here”);
System.out.println(list.get(0).toUpperCase());
False
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
d.)
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();
b.), d.), and e.)
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
d.)
What is a method signature?
A method’s signature is comprised of the method’s name and the method’s input parameters.
In your own words, explain and give an example of overloading
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);