Sie sind auf Seite 1von 4

Advanced Programming Lecture Quiz 1 – 2/11/2017

Advanced Programming – Lecture Quiz 1


Q1. Identify the primitives, references, classes and objects in the following Java code:

double d = 5.0;
int i[] = {1,2,3,4};
List l = new List();
Double k = new Double();
Tree t;
float f;
Computer c = null;

Q2. Correct the following code:


1. import java.Scanner;
2. public class Output {
3. public static void main( String args[] )
4. int num
5. Scanner input = Scanner( in );
6. num = input.int();
7. }
8. }

Q3. In this question, I show you the signatures of some constructors and methods that could be
defined for a Bank class, a class intended to store information about a bank. I would like you to show
me how you would call the methods. If the method or constructor returns a value, I would like you to
put the call on the right hand side of an assignment statement so that the returned result is saved in a
variable. For the purposes of this question, you can assume that the following variables have been
declared and initialized, if necessary:

Bank bank;
String bankName;
String bankAddress;
int id;
Person accountOwner;

1
Advanced Programming Lecture Quiz 1 – 2/11/2017

String ownerName;
Account[ ] accountList;
Do not provide an implementation for these methods.
a. public Bank (String name)
b. public String getAddress()
c. public void printAccountsList()
d. public void setClient (Person owner, int accountID)
e. public Account[] getAccounts ()

Q4. For each term in the left column, write the letter for the description from the right column that
best matches the term.

class declaration ( ) a. Used in a class instance creation expression to create instance of


a class.
calling method ( ) b. Default initial value of
reference ( ) c. Causes Java to execute a method
new keyword ( ) d. A variable that refers to an object contains one of these
null ( ) e. Encompasses all of the attributes and behaviors of a class

Q5. MCQ
1. Consider the following code fragment
Rectangle r1 = new Rectangle();
r1.setColor(Color.blue);
Rectangle r2 = r1;
r2.setColor(Color.red);
After the above piece of code is executed, what are the colors of r1 and r2 (in this order)?
(a) Color.blue
Color.red
(b) Color.blue
Color.blue
(c) Color.red
Color.red

2
Advanced Programming Lecture Quiz 1 – 2/11/2017

(d) Color.red
Color.blue
(e) None of the above.

2. A constructor
(a) Must have the same name as the class it is declared within.
(b) Is used to create objects.
(c) May be declared private
(d) Both (a) and (b) above
(e) (a), (b) and (c) above.

3. You read the following statement in a Java program that compiles and executes.
submarine.dive(depth);
What can you say for sure?
(a) depth must be an int
(b) dive must be a method.
(c) dive must be the name of an instance field.
(d) submarine must be the name of a class
(e) submarine must be a method.

4. Consider,
public class MyClass
{
public MyClass(){/*code*/}
// more code...
}
To instantiate MyClass, you would write?

3
Advanced Programming Lecture Quiz 1 – 2/11/2017

a) MyClass mc = new MyClass();


b) MyClass mc = MyClass();
c) MyClass mc = MyClass;
d) MyClass mc = new MyClass;
e) The constructor of MyClass should be defined as, public void MyClass(){/*code*/}.

5. Which statement is true regarding an object?


a) An object is what classes instantiated are from
b) An object is an instance of a class
c) An object is a variable
d) An object is a reference to an attribute
e) An object is not an instance of a class.

Das könnte Ihnen auch gefallen