Www.cs.umd.edu



Passport (Intermediate) Final Exam Practice Questions

The following exercises will help you prepare for the final exam. Solutions to these exercises will not be provided, but you are welcome to discuss your solutions with the TAs (Teaching Assistants). You do not need to hand in these exercises. Some of the questions can be considered challenging.

Problem 1 (General Questions)

a. What is the difference between System.out.println and System.out.print ?

b. What is the heap?

c. What is the difference between an object and a class?

d. Is the heap an infinite memory area? Explain.

e. What is garbage collection?

f. Are the following assignments valid?

int age = 17;

double temp = 3.4;

String m = “John” + age + “/” + temp;

g. What is a reference variable?

h. What is the difference between a reference and a primitive variable?

i. How many reference variables can be associated with an object?

j. In the following code fragment, how many objects do we have at every step?

String r;

String s; // Step 1

r = s = “Happy”; // Step2

r = new String(s); // Step 3

r = null; // Step 4

r = new String(“John”); // Step 5

k. When do we use the String equals method?

l. What would happen when you execute the following code fragment?

String k = null;

System.out.println(k);

System.out.println(k.length());

m. Define a Java interface with a method that has the following prototype:

public void print();

After defining the interface, define a class that implements the interface. The method will print the message “Hello”.

Problem 2

A memory map is a representation of the values of variables, and the objects we have in a program. For example, given the following code segment:

public static void main(String[] args) {

int x;

x = 2;

String p;

p = new String(“Test”);

String t = null;

// STOP HERE

}

the memory map we have until the point right before the method finishes (indicated by the comment // STOP HERE) is:

Stack Heap

[pic]

As you can see we represent references (addresses) with arrows, objects with rectangles, and the null value with a line that has a perpendicular line at its endpoint. The stack is where the method’s local variables reside; the heap is any area to the right of the stack.

Using the above memory map approach draw a memory map for each of the following:

a. Problem 1.j above after finishing Step 5.

b. For the following method until we reached the point marked // STOP HERE

public class Test {

public static void main(String[] args) {

String p = new String("House");

String m;

double cost = 3.4;

m = new String(p);

m = null;

m = p;

p = new String("Compiler");

// STOP HERE

}

}

Problem 3

The next two classes will be used to answer the questions that follow.

public class MethodsExample {

public void printName(String name) {

if (validName(name)) {

System.out.println("Welcome: " + name);

} else {

System.out.println("Who are you?");

}

}

private boolean validName(String name) {

if (name.equals("John") || name.equals("Mary"))

return true;

else

return false;

}

public void printAll(String name, int age) {

printName(name);

System.out.println("Age: " + age);

}

}

public class Driver {

public static void main(String[] args) {

// CODE SEGMENT

}

}

a. In the MethodsExample class the printName method calls the validName method. Can all the

methods of the class call each other? Does it make a difference whether the method is public

or private? Does it make a difference the methods’ declaration order?

b. Indicate whether each of the following code segments are valid or invalid. Each code segment

represents the body of the main method in the Driver class (where the //CODE SEGMENT

comment appears).

i. MethodsExample e = new MethodsExample();

e.printAll("John", 20);

ii. MethodsExample e = new MethodsExample();

e.validName("John");

iii. MethodsExample e;

e.printName("John");

iv. new MethodsExample().printAll("Mary", 16);

v. Revisit i. through iv. but assume all methods of the class MethodsExample are public.

vi. Revisit i. through iv. but assume all methods of the class MethodsExample are private.

Problem 4

Define a Student class that represents a college student. A student has a name (String reference), a gpa (double), and an id (integer). The class has the following methods that you must consider non-static unless indicated otherwise.

1. Default constructor – Initializes the name, gpa, and id variables with the values “NONAME”, 0.0, and -1 respectively.

2. Constructor – Takes a name, gpa, and id values as parameters and initializes the current object accordingly.

3. equals – Determines whether the current object and the single parameter value represent the same student.

4. increaseGPA – Increases the student gpa by the single parameter value.

5. toString – Generates a String with all the values associated with a student object using the format: “Student: “ followed by the name, gpa and id (each separated by a blank character).

6. Write the appropriate accessor and mutators methods associated with the class. For example, getId is the accessor for id and setId the mutator.

7. Write a copy constructor for your Student class.

8. Write a method called notEquals which uses the equals method you defined above.

9. Write a static method called total that returns the number of Student object instances that have been created. Feel free to add any instance variables that will allow you to keep track of the number of Student objects created.

Problem 5

A Stack is a data structure frequently used in Computer Science. In this structure elements are inserted and removed from the same end (similar to a stack of clean plates in a cafeteria). Implement a class called MyStack which represents a Stack of String objects. The specifications associated with the class are:

Instance variable

String[] array; // Array used to represent the stack.

// Feel free to add any other variables you may need.

Methods

• Constructor – Creates a stack with no elements. Hint: create an array with size 0.

• push – Adds an element to the top of the stack. It takes as parameter a reference to

a string. The method will create a duplicate of the string and will add the copy to

the stack. The array must be resized to accommodate the new element.

• pop – Removes an element from the top of the stack. It takes no parameters. The method removes the string at the top of the stack and resizes the array. The method returns a reference to the string that was removed.

• top – Returns a reference to the element at the top of the stack without removing the element.

• empty – Returns true if the stack is empty and false otherwise.

The following illustrates how to use the class MyStack:

MyStack myStack = new MyStack();

myStack.push(“Hello”);

myStack.push(“Everybody”);

System.out.println(()); // It will print “Hello”.

myStack.pop();

System.out.println(()); // It will print “Everybody”.

Problem 5

A method name filter has the following prototype:

public static int[] filter(int[] data, int maxValue);

The method returns an integer array with elements from the data array that have a value less than or equal to maxValue.

-----------------------

x

p

2

Test

t

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download