CS52

¡Supera tus tareas y exámenes ahora con Quizwiz!

In order to make a user-defined ADT available that is defined in the file myfile.h, you would

#include "myfile.h"

Which include directive is necessary for file IO?

#include <fstream>

Which of the following symbols has the highest precedence?

%

What is the correct way to write the condition y < x < z?

((y < x) && (x < z))

Which of the following boolean expressions tests to see if x is between 2 and 15 (including 2 and 15)?

(x >=2 && x <=15)

What is the output of the following function call? //function body int factorial(int n) { int product=0; while(n > 0) { product = product * n; n—; } return product; } //function call cout << factorial(4);

0

What is the value of i after the following function call? //function definition int doSomething(int value) { value = 35; return value; value = 13 } //fragment of main program int i=0; cout << doSomething(i);

0

What is the output of the following code fragement? double size, volume=16.0; size = sqrt(sqrt(volume)) / 3; cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); cout << size;

0.67

Given the function definition void something ( int a, int& b ) { int c; c = a + 2; a = a * 3; b = c + a; } What is the output of the following code fragment that invokes something? int r = 1; int s = 2; int t = 3; something(t, s); cout << r << ' ' << s << ' ' << t << endl;

1 14 3

What is the value of x after the following statements? double x; x = 0; x += 3.0 * 4.0; x -= 2.0;

10.0

What is the output of the following code fragment? int x=13; cout << x <<","

13,

What is the value of x after the following statements? int x; x = 15 %4;

3

Given the following function definitions and program fragments, what is the output? void f1(int& z, int &q) { int temp; temp=q; q=z; z=temp; } void f2( int& a, int& b) { if( a<b) f1(a,b); else a=b; } int x=3, y=4; f2(y,x); cout << x <<" " << y << endl;

3 3

What is the value of x after the following statements? float x; x = 15/4;

3.0

Another way to write the value 3452211903 is

3.452211903e09

What is the value of x after the following statement? float x; x = 3.0 / 4.0 + 3 + 2 / 5;

3.75

What is the value of x after the following statements? int x, y, z; y = 10; z = 3; x = y * z + 3;

33

Given the following code fragment and the input value of 4.0, what output is generated? float tax; float total; cout << "enter the cost of the item\n"; cin >> total; if ( total >= 3.0) { tax = 0.10; cout << total + (total * tax) << endl; } else { cout << total << endl; }

4.4

What is the value returned by the following function? int function() { int value = 35; return value + 5; value += 10; }

40

Given the following code, what is the final value of i? int i; for(i=0; i<=4;i++) { cout << i << endl; }

5

What is the value of x after the following code fragment executes? float x = 36.0; x = sqrt(x);

6.0

A namespace is

A and C

An algorithm is

A finite set of steps to solve a problem

Which boolean operation is described by the following table?

AND

ADTs should be in separate files because

All of the above

Who developed C++?

Bjarne Stroustrup

From which language did C++ directly evolve?

C

A stub is a function that is completely defined and well tested.

F

A void function can be used in an argument.

F

All constructors for a class must be private.

F

Data that is sent to an output stream representing a file will be immediately saved to disk.

F

The assignment operator may not be used with objects of a class.

F

Two different objects of the same class have a different set of member functions.

F

A break statement in a switch stops your program.

False

A function may return more than one item.

False

All operators can be overloaded.

False

Classes must always be defined in separate files.

False

Every line in a program should have a comment.

False

Friend functions are members of the class.

False

Functions that are constant member functions may call the class mutator functions.

False

In an enumerated data type, different constants may not have the same value.

False

It is illegal to make function calls inside a switch statement.

False

Operators must be friends of the class.

False

The break statement causes all loops to exit.

False

The following statement is legal: cout >> "Hello, my name is Bill\n";

False

The integer 0 is considered true.

False

The opposite of (x >3 && x < 10) is (x < 3 && x > 10).

False

There are 8 bytes in a bit.

False

You should write your program before you write the algorithm.

False

pow(2,3) is the same as pow(3,2).

False

What is the value of x after the following statements? int x; x = x + 30;

Garbage

Given the following class definition, how would you declare an object of the class, so that the object automatically called the default constructor? class ItemClass { public: ItemClass(); ItemClass(int newSize, float newCost); int getSize(); float getCost(); void setSize(int newSize); void setCost(float newCost); private: int size; float cost; };

ItemClass myItem;

Which of the following is true for a void function?

Nothing is returned.

If you have the two functions as shown, int someFunction(int value); float someFunction(float value); and a variable x, which is a double, which function is called by the following statement? cout << someFunction(x);

Nothing, it is a syntax error

A using directive that appears inside a set of braces applies

Only to that block

Which of the following is not an example of a program bug?

Operator error

Which of the following would be an appropriate function declaration to add two rational numbers?

Rational operator + ( const Rational &left, const Rational &right);

A derived class is more specific than its parent, or base class.

T

Functions can return at most one value.

T

Streams may be passed to a function.

T

The following is a properly declared overloaded insertion operator for myClass. ostream& operator <<(ostream &out, const myClass &obj);

T

Two different structure definitions may have the same member names.

T

Given the following code fragment, what is the output? int x=5; if( x > 5) cout << "x is bigger than 5. "; cout <<"That is all. "; cout << "Goodbye\n";

That is all. Goodbye

Why will the following code not compile? namespace ns1 { void print(); void display1(){}; } namespace ns2 { void print(); void display2(){}; } int main() { using namespace ns1; using namespace ns2; display1(); display2(); print(); return 0; }

The call to print is ambiguous.

float tax = 0.0, subtotal = 0.0; //code fragment calculateCost(15, subtotal, tax); cout << "The cost for 15 items is " << subtotal << ", and the tax for " << subtotal << " is " << tax << endl; //end of fragment void calculateCost(int count, float& subTotal, float taxCost) { if ( count < 10) { subTotal = count * 0.50; } else { subTotal = count * 0.20; } taxCost = 0.1 * subTotal; }

The cost for 15 items is 3.00, and the tax for 3.00 is 0.00.

Given the following class, what is syntactically wrong with the implementation of the display function? class Rational { public: Rational(); Rational(int numer, int denom); Rational(int whole); int getNumerator(); int getDenominator(); friend void display(ostream& out, const Rational& value); private: int numerator; int denominator; }; void display(ostream& out, const Rational& value) { out << value.getNumerator() << '/"<<value.getDenominator(); }

The get functions are not const functions

If c is a character variable that contains a digit, what does the following function return? int digit_to_int(char c)

The integer equivalent of the digit stored in c

When overloading a function, what must be true?

The names should be the same with different number and/or types of parameters.

What does the following line of code display to the screen? cout << "This is the computer\nprogramming book\n";

This is the computer programming book

A semicolon by itself is a valid C++ statement.

True

All code is in some namespace.

True

All switch statements can be converted into nested if-else statements.

True

Function naming rules follow variable naming rules.

True

Functions may have multiple return statements.

True

Grace Hopper discovered the first documented computer bug.

True

In a program with no user defined namespaces, all names are defined in the global namespace.

True

It is legal to declare more than one variable in a single statement.

True

The body of a do-while loop always executes at least once.

True

Variables that are declared outside of any function body or parameter list are considered global.

True

You cannot create new operators.

True

You may not change the precedence of operators by overloading them.

True

What is wrong with the following code? while( ! fileIn.eof() ) { fileIn >> value; fileOut << value; }

We have read past the end of the input file and attempt to output a nonexistent value

The precondition(s) for a function describes

What must be true before the function executes

Call-by-reference should be used

When the function needs to change the value of one or more arguments

Given the following class, what would be the best declaration for a constructor that would allow the user to initialize the object with an initial age and cost? class Wine { public: Wine(); int getAge(); float getCost(); private: int age; float cost; }

Wine(int newAge, float newCost);

Information hiding is analogous to

a black-box methodology

A class member function that automatically initializes the data members of a class is called

a constructor

A member function that allow the user of the class to change the value in a data member is known as

a mutator function

A data type consisting of data members and operations on those members which can be used by a programmer without knowing the implementation details of the data type is called

an abstract data type

What is wrong with the following switch statement? int ans; cout <<"Type y for yes on n for no\n"; cin >> ans; switch (ans) { case 'y': case 'Y': cout << "You said yes\n"; break; case 'n': case 'N': cout << "You said no\n"; break; default: cout <<"invalid answer\n"; }

ans is an int

Which of the following lines correctly reads a value from the keyboard and stores it in the variable named myFloat?

cin >> myFloat

functions pow(), sqrt(), and fabs() are found in which include file?

cmath

Since accessor functions in a class do not modify or mutate the data members of the object, the function should have the __________ modifier.

const

If you have the following variable declaration in your program, const int SIZE=34; then which of the following statements are legal?

cout << SIZE;

Given the following class and object declaration, how would you print out the age and cost of a bottle of wine? class Wine { public: Wine(); int getAge(); float getCost(); private: int age; float cost; } Wine bottle;

cout << bottle.getAge() << bottle.getCost();

Given the following class and array declaration, how would you print out the age of the 10th person in the array? class personClass { public: void setAge(int newAge); void setGender( char newGender); void setSalary(float newSalary); int getAge(); char getGender(); float getSalary(); private: int age; char gender; float salary; }; personClass people[100];

cout << people[9].getAge();

Given the following strucure definitions, what is the correct way to print the person's birth year? struct DateType { int day; int month; int year; } struct PersonType { int age; float weight; DateType birthday; } PersonType person;

cout << person.birthday.year;

if x is 0, what is the value of (!x ==0)?

false

Given the following code fragment, which of the following expressions is always true? int x; cin >> x;

if( x = 1)

If the name of the input file was in a variable named filename, which of the following is the correct way to open the input stream named inFile and associate it with this file?

inFile.open(filename);

What is the output of the following code fragment? int x=0; while( x < 5) cout << x << endl; x ++; cout << x << endl;

indeterminate

What is wrong with the following for loop? for(int i=0;i<10;i--) { cout << "Hello\n"; }

infinite loop

If you need to write a function that will compute the cost of some candy, where each piece costs 25 cents, which would be an appropriate function declaration?

int calculateCost(int count);

Given the following class definition, how could you use the constructor to assign values to an object of this class? class CDAccount { public: CDAccount(); CDAccount(float interest, float newBalance); float getBalance(); float getRate(); void setRate(float interest); void setBalance(float newBalance); private: float balance, rate; }; and the following object declaration CDAccount myAccount;

myAccount = CDAccount(myRate, myBalance);

In order to create a namespace called student, you use

namespace student { //code goes here }

What is wrong with the following function body? void calculate(int count, float price, float& cost) { if (count < 0) cost=0.0; else cost=count*price; return; }

nothing

The get function reads

one character value

To overload functions with symbolic names (like + - / <<), you must use the keyword _______ before the symbolic name.

operator

Given the following class definition and the following member function header, which is the correct way to output the private data? class Person { public: void outputPerson(ostream& out); private: int age; float weight; int id; }; void Person::outputPerson(ostream& out) { //what goes here? }

out << age << weight << id;

In the function declaration shown, the mechanism used to call this function is known as: double pow(double base, double exp);

pass by value

In a class, all members are ____________ by default

private

Which of the following is not a valid identifier?

return

All the code between #ifndef MYCLASS_H and #endif is ____________ if MYCLASS_H is defined.

skipped

In an overloaded insertion or extraction operator, which object should be the first parameter, the stream or the object of the class?

the stream

In order to hide functions that are defined in the implementation file, they should be part of the ______________ namespace.

unnamed

Which of the following are valid declarations for an assignment operator for a class named myClass?

void operator = (const myClass& source);

Which of the following function declarations will accept either cout or a file stream object as its argument?

void output( ostream &outFile);

Given the following class, what would be the best declaration for a mutator function that allows the user of the class to change the age? class Wine { public: Wine(); int getAge(); float getCost(); private: int age; float cost; }

void setAge(int newAge);

The destructor for a class is called

when the object of the class goes out of scope

using namespace std; tells the compiler

where to get the definitions of certain objects (variables)


Conjuntos de estudio relacionados

Chemistry Semester 2 Test 3 Mini Test

View Set

Female & Male Reproductive problems DYNAMIC ATI

View Set

Deferred Revenue & Expenses and Accrued Revenues & Expenses

View Set

Foundations for Living B 2018 : 2. CHRISTIAN EDUCATION

View Set