2. (TCO 2) What is wrong with the following switch statement?
Switch(x)
{
case 0:
cout << 2/x;
case 2:
cout << 2/x;
break;
default:
cout << “error”;
break;
}
__ The value of x could be something other than 1 or 2. (false, others would default)
__ There is no break statement for case 1. (Malik pp. 217; must have break )
__ There is no semicolon after the closing bracket. (not necessary)
__ All of the above.
3. (TCO 2) Which operation in the following expression will be performed first?
c = a++ / b + 5;
__ a++ (Malik, pp. 1277 ++ then / then +)
__ a/b
__ assignment to c
__ b + 5
4. (TCO 2) For a switch statement, which of the following case statements expressions is invalid or logically incorrect?
__ case 1 < x < 100;
__ case ‘B’|’b’;
__ case a23.45
__ All of the above (case labels cannot be expressions or alphanumeric)
5. (TCO 2) What is the value of i after the following code fragment executes?
Int i = 2;
Int k = 5;
i *= k + 1;
__ 7
__ 11
__ 12 ( the evaluation is the same as i = i * (k + 1) so i = 2 * (5 + 1) = 12)
__ 14
6. (TCO 2) What is the data type of bullpen[3].era?
Struct pitcher
{
String name;
double era;
};
pitcher p1;
pitcher bullpen[10];
__ string
__ double
__ pitcher
__ const pointer to pitcher
7. (TCO 2) Given the following code fragment, what is the data type of roster[9] .name?
struct student
{
string name;
double gpa;
};
student thisStudent;
student roster[50];
__ string
__ const pointer to student
__ student
__ double
8. (TCO 2) What is the declaration for a C-style string that can hold a name with up to 3 printable characters (for example, “Ron”)?
__ int name [3];
__ char name [3];
__ int name [4];
__ char name [4];
9. (TCO 2) Which of the following statements calls the following function correctly?
Int MultiplyValues (int, int);
__ int a = MultiplyValues (int x, int y);
__ int a = MultiplyValues (10, 20);
__ double d = MultiplyValues (int 10, int 20);
__ All of the above
10. (TCO 2) Which of the following is an invalid declaration to overload the following function?
double foo(int);
__ double foo(double);
__ int foo(int);
__ double foo(double *):
__ double foo(int *);
11. (TCO 2) Which of the following functions is taking an array of MyStruct structures as a pass-by-value parameter?
__ void MyFunc(MyStruct data[ ] );
__ void MyFunc(MyStruct &data[ ] );
__ void MyFunc(MyStruct *data [ ] );
__ not allowed in C++
1. (TCO 2) Composition is typically an example of:
__ a “has a” relationship (Composition, Malik pp. 724)
__ an “is a” relationship (Inheritance, Malik pp. 724)
__ a “uses a” relationship
__ an “is used” relationship
2. (TCO 2) When organizing a program into three files (main, class implementation, and class header). Where are the user-defined data types declared?
__ main.cpp
__ class.cpp
__ class.h ?
__ main.h
3. (TCO 2) Which of the following is called automatically each time an object Is created?
__ Compiler
__ Builder
__ Constructor
__ Destructor
4. (TCO 2) Given the following class definition and lines of code, Line 1 in main is a call to what?
class Distance
{
Private:
int feet;
double inches;
public:
Distance ( );
Distance (int initFt, double initIn);
void setFeet (int feetIn);
void setInches (double inchesIn);
int getFeet ( ) const;
double getInches ( ) const:
};
int main ( )
{
Distance d1; //Line 1
const int MAX = 100; //Line 2
Distance list [MAX]; //Line 3
Distance d2 (1, 2.3); //Line 4
Distance * pDist; //Line 5
D1.feet = 5; //Line 6
// ect. – assume that the remaining code is correct
}
__ The 0-argument Distance constructor.
__ The 2-argument, int, double, Distance constructor.
__ The 2-argument, double, int, Distance constructor.
__ The 1-argument, int, Distance constructor.
5. (TCO 2) Variables defined to be of a user-declared class are referred to as:
__ attributes
__ member variables
__ primitive variables
__ objects
6. (TCO 2) What is the output of the following code snippet?
int value = 10;
int * iptr = &value;
*iptr = 5;
Cout << *iptr << “ “ << value;
__ 5 5
__ 10 10
__ 5 10
__ 10 5
7. (TCO 2) What is the output of the following code snippet?
int *list = new int [5];
int *ptr;
for (int i = 0; i < 5; i++)
list [ i ] = i + 1;
ptr = list;
delete [ ] list;
cout << *ptr;
__ 1
__ address of list
__ address of ptr
__ error – ptr references memory which no longer belongs to the program
8. (TCO 2) Which of the following is not a legal value to initialize a pointer variable?
__ zero
__ null
__ 0
__ the address of a variable of the same type
9. (TCO 2) Which of the following Java statements creates an object of a ButtonHandler calss (which implements the ItemListener interface) and associates it with a JRadioButton object dalled buttonOne?
10. (TCO 2) What Java package contains the definitions of the Java event listener interfaces?
__ java.events
__ java.GUI.events
__ java.awt.event
__ java.event.listener
11. (TCO 2) Which of the following creates a multiline display which is user editable?
__ JLabel display = new JLabel(“Enter text”);
__ JTextField display = new JTextField(“Enter text”);
__ JTextArea display = new JTextArea(20, 20);
__ JScrollPane display = new JScrollPane( );
12. (TCO 2) In the event of an error in a Java program, what class and method would you use to display a message box indicating that an error had occurred?
__ Use the showMessageBox method of the System.out class.
__ Use the showInputDialog method of the JOptionPane class.
__ Use the show Error Message method of the JDialog class.
__ Use the showMessageDialog method of the JOptionPane class.
13. (TCO 2) The x, y drawing coordinate system used in Java graphics
__ uses character units.
__ has the origin in the upper left corner of the display area.
__ uses the x coordinate for the verticle axis.
__ All of the above
14. (TCO 2) Which of the following Java statements gets an integer value from the user?
Public class MyApp extends JFrame implements ActionListener {….}
__ int x = Integer.parseInt(JOptionPane.showInputDialog(“Enter an integer value”));
__ int x = JOptionPane.getInteger(“Enter an integer value”);
__ int x = Scanner.getInt(“Enter an integer value”);
__ int x = String.toInt(JOptionPane.showMessageDialog(“Enter an integer value”));