CS100A Sample #1 Prelim 1
Question 1 (18 points). For each of the statements below, show the new values of any variables whose values change after execution of the statement. (We have provided two examples to show you what we mean.) Variables a, b, c are of type int, and x and y are of type boolean. Here are the values of the variables before each statement is executed:
a: 5 b: -6 c: 15 x: true y: false
b = b + 1; Answer: b is -5
(a) Define the term class.
(b) Explain the difference between public, protected, and private.
(c) Define the term function, as we have used it, and give an example of a function declaration (you don’t need to write class declarations and all; just write the declaration of your example function, i.e., just the function header and body).
(d) In class C below, do the following. (1) Circle each variable reference that is illegal. (2) For each legal reference, draw an arrow from it to the declaration of the local variable, parameter, or field to which it refers. Don’t circle or draw arrows from the declarations. We drew one arrow to show you what we mean. Don’t concern yourself with what the class is for; its only purpose is to test knowledge of scope rules.
public class C
{private int x;
public int y;
public C(int x, int a)
{x= a;
y= x;
}
public String toString()
{x= 5; z= 6;
int x= 4;
int z= 7;
x= y;
}
}