CS 101 Fall 2004 Test 2: Solutions for Logical Operations and Variables - Prof. Aaron Bloo, Exams of Computer Science

The solutions for test 2 of cs 101 fall 2004, focusing on logical operations and variables. It includes truth tables, code segment outputs, and questions related to conditional statements and object instantiation.

Typology: Exams

Pre 2010

Uploaded on 03/09/2009

koofers-user-ku0
koofers-user-ku0 🇺🇸

10 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Fall 2004 - CS 101: Test 2 Name ______________________________________UVA Email ID _________
Pledged Page 1 of 8 Pledged
Grading
1. (4 points) What is your course section?
CS 101 CS 101E
Page 1 / 4
Page3 / 20
Page 4 / 13
Page 5 / 10
Page 6 / 26
Page 7 / 17
Page 8 / 10
Total / 100
pf3
pf4
pf5
pf8

Partial preview of the text

Download CS 101 Fall 2004 Test 2: Solutions for Logical Operations and Variables - Prof. Aaron Bloo and more Exams Computer Science in PDF only on Docsity!

Grading

1. (4 points) What is your course section?

CS 101 CS 101E

Page 1 / 4

Page3 / 20

Page 4 / 13

Page 5 / 10

Page 6 / 26

Page 7 / 17

Page 8 / 10

Total / 100

The following definitions are in effect throughout the remainder of the test.

public class A { private int v1; private int v2; public A(int f1, int f2) { this.v1 = f1; this.v2 = f2; } public String toString() { return "( v1 = " + v1 + " " + v2 + ")"; } }

public class B { private int b; public B() { System.out.println("b = " + b); } }

public Class C { private int c1; static private int c2; public C() { c1 = 0; c2 = 0; } public void increment() { ++c1; ++c2; } public String toString() { return "( c1 = " + c1 + " c2 = " + c2 + ")"; } }

6. (3 points) Consider the following code segment.

int n1 = 12; int n2 = 144; if (n2 < n1) { n1 = 10; } System.out.println("n1 = " + n1);

What is its output?

n1 =

7. (3 points) Consider the following code segment.

String s1 = "wahoo"; String s2 = "wahoo"; if (s2 == s1) { s1 = "UVA"; } System.out.println("s1 = " + s1);

What is its output?

s1 =

8. (3 points) Consider the following code segment.

String s1 = new String("wahoo"); String s2 = new String("wahoo"); if (s2.equals(s1)) { s1 = "UVA"; } System.out.println("s1 = " + s1);

What is its output?

s1 =

9. (4 points) Consider the following code segment?

C n1 = new C(); C n2 = new C(); n1.increment(); n2.increment() System.out.println( n1.toString() ); System.out.println( n2.toString() );

What is its output?

( c1 = c2 = )

( c1 = c2 = )

10. (4 points) Does the following program compile? Why?

public class DemoB { public static void main(String[] args) { B b1 = new B(); B b2 = b1; int v = b1.b; System.out.println( v ); } }

11. (2 points) Does the following program compile? Why?

public class DemoA { public static void main(String[] args) { A a = new A(); System.out.println( a ); } }

12. (4 points) Rewrite the following code segment using proper indentation.

if (n == 1) { i = 1; } else { if (n != 2) { i = 2; } else { i = 3; } }

18. (6 points) Write an appropriately named mutator for class A, which has a single int parameter f1. The

mutator sets the v1 attribute of the invoking object to f1.

19. (7 points) Write an equals() method for class A. The method should return true if and only if the

actual parameter is an A object whose attributes match the attributes of the invoking object.

20. (4 points) Complete the following code segment in the following manner.

  • a1 references a new default constructed A object.
  • a2 references a new A object constructed so that its attributes both equal 10.

A a1 =

A a2 =

21. (10 points) Write a program named ProcessTwo.java. The program prompts and extracts an integer

number from standard input. If the first number is less than 5 then the program displays "Wahoo"; oth-

erwise it displays "Cavalier". Commenting and input echoing is not necessary.

PLEDGE :