Docsity

Midterm Exam - Object-Oriented Programming | CS 211, Exams of Computer Science

Toggle sidebar
bg1
1. What is displayed by the following code?
System.out.println(“values: “ + 5 + 19);
System.out.println(“values: “ + (5 + 19);
System.out.println((“values: “ + 5) + 19);
2. What is displayed by the code:
a1 = new Arf(7);
a2 = new Arf(100);
a1.setWoof(a2.getWoof());
a3 = a2;
a2.setWoof(a3.getWoof() + 9);
System.out.println(a1.getWoof() + “, “ + a2.getWoof()
+ “, “ + a3.getWoof());
if arf is defined by:
public class Arf
{
private int woof;
public Arf(int i)
{
woof = i;
}
public int getWoof()
{
return woof;
}
public void setWoof(int i)
{
woof = i;
}
}
3. Discuss how a static field differs from an ordinary (non-static) field.
4. Write a method which creates an instance of Random, uses it to create 100 random values, each of
which is 0 or 1, then prints out the number of 0s generated and the number of 1s generated.
5. Write a class which has two private int fields called one and two. Your class must have a constructor
which takes two int parameters which it uses to initialize the fields, and “get and set” methods for both
fields. You should also provide two methods (which can be called from methods in other classes), one
which returns the sum of the fields and one which returns the product of the fields.

Partial preview of the text

Download Midterm Exam - Object-Oriented Programming | CS 211 and more Exams Computer Science in PDF only on Docsity!

  1. What is displayed by the following code?

System.out.println(“values: “ + 5 + 19); System.out.println(“values: “ + (5 + 19); System.out.println((“values: “ + 5) + 19);

  1. What is displayed by the code:

a1 = new Arf(7); a2 = new Arf(100); a1.setWoof(a2.getWoof()); a3 = a2; a2.setWoof(a3.getWoof() + 9); System.out.println(a1.getWoof() + “, “ + a2.getWoof()

  • “, “ + a3.getWoof());

if arf is defined by:

public class Arf { private int woof;

public Arf(int i) { woof = i; } public int getWoof() { return woof; } public void setWoof(int i) { woof = i; } }

  1. Discuss how a static field differs from an ordinary (non-static) field.
  2. Write a method which creates an instance of Random, uses it to create 100 random values, each of which is 0 or 1, then prints out the number of 0s generated and the number of 1s generated.
  3. Write a class which has two private int fields called one and two. Your class must have a constructor which takes two int parameters which it uses to initialize the fields, and “get and set” methods for both fields. You should also provide two methods (which can be called from methods in other classes), one which returns the sum of the fields and one which returns the product of the fields.