CS Exam 1

Ace your homework & exams now with Quizwiz!

What is output when ch stores the value 'b'? switch (ch) { case 'a': case 'A': cout << "Option A "; break; case 'b': case 'B': cout << "Option B "; break; case 'c': case 'C': cout << "Option C "; break; default: cout << "Invalid choice"; } A) Option B B) Option B Option C C) Option B Option C Invalid choice D) Invalid choice E) Nothing is output

A

What is output when the following code is executed? int x = 7; while (x <= 5) { cout << "Repeat " ; x = x + 1; } cout << "Done!" << endl; A) Done! B) Repeat Done! C) Repeat Repeat Repeat Repeat Repeat Done! D) Something else

A

What is the value of donuts after the following code executes if the user types 10? int donuts; cin >> donuts; if (donuts > 8) donuts = 0; else donuts = donuts + 2; A) 0 B) 2 C) 8 D) 10 E) 12

A

When a variable of type int is multiplied by a variable of type float, as in the following, what happens? int years = 25; float interestRate = 1.5; cout << years * interestRate; A) years will be promoted to a float B) interestRate will be promoted to an int C) years and interestRate will both be promoted to double D) neither variable will be promoted

A

Which of the following is an example of a Named Constant? A) const int x = 10; B) int x = 10; C) int x; D) 10 E) all of the above

A

Which of the following is an example of integer division in C++? A) x=101/11; B) x=3/1.5; C) x=101%11; D) x=335%10;

A

What will be the value of input_value after this code is executed if the value 6 is entered at the keyboard? int input_value; cin >> input_value; if (input_value > 5) input_value = input_value + 5; else if (input_value > 2) input_value = input_value + 10; else input_value = input_value + 12; A) 6 B) 11 C) 16 D) 18 E) 21 F) 33

B

Which statement should go after the following statements to input the user's major, Computer Science? string major; cout << "Please enter your major (no abbreviations): " << endl; A) cin >> major; B) getline(cin,major); C) Both of these will work. D) None of these will work.

B

A _______ is declared outside of all functions. A) local variable B) global variable C) reference variable D) counter variable

b

A function __________ contains the statements that make up the function. A) call B) definition C) prototype D) parameter E) instance

b

During the execution of a function, a reference parameter, such as int &t, A) contains a copy of the argument. B) is an alias to the argument. C) both are true D) neither are true

b

Given the following program: int cube(int x) { return x * x * x; } int main() { int result; //here } Which statement below can replace the comment //here in main, to call the cube function, passing the value 4 to it, and assigning the function's return value to result? A) cube(result); B) result = cube(4); C) cube(4); D) cube(4) = result; E) result = 4*4*4;

b

What is output by the following code? void calc (int a, int& b) { int c; c = b + 1; a = a * 3; b = c + a; } int main() { int x = 2, y = 5; calc(x, y); cout << x << " " << y << endl; } A) 2 5 B) 2 12 C) 6 5 D) 6 12

b

What is output by the following code? void func1 (int &x) { // Note & x=57; } int main { int m = 25; func1(m); cout << "m is " << m <<endl; } A) m is 25 B) m is 57 C) m is 58 D) Nothing is output

b

What is output by the following statements? int z[] = {11, 12, 13, 15, 14, 10}; int m = z[0]; for (int i=1; i<5; i++) { if (z[i] < m) m = z[i]; } cout << "It's " << m << endl; A) It's 10 B) It's 11 C) It's 15 D) Something else

b

What is the error in the following C++ code? struct Pair { int a; int b; }; int main () { Pair.a = 10; Pair.b = 20; } A) A structure cannot have two members of the same type. B) Pair.a is invalid because Pair is not a struct variable. C) Pair.a should be Pair[a] D) There is no error in this code.

b

What is the subscript of the last element in the following array? int values[10]; A) 0 B) 9 C) 10 D) 11

b

What is the value of the variable x after executing the following statement? float x = 13/4; A) 1.75 B) 3.0 C) 3.25 D) 3.3

b

What will be output by the following statements? int score = 40; if (score > 95) cout << "Congratulations! "; cout << "That's a high score!"; A) Congratulations! B) Congratulations! That's a high score! C) That's a high score! D) There is no output.

C

Which answer below best describes what is output to the screen when the following code is executed inside a program? cout << "Monday"; cout << "Tuesday"; cout << "Wednesday"; A) Monday Tuesday Wednesday (each word on a separate line) B) Monday Tuesday Wednesday (all on the same line) C) MondayTuesdayWednesday (all on the same line, no spaces) D) "Monday" "Tuesday" "Wednesday" (each word on a separate line) E)None of the above

C

What is the output of the following program? int main () { int x, y, z; x = y = z = 4; x = x + 1; y += 2; z /= 2; cout << x << " " << y << " " << z << endl; } A) 4 4 4 B) 1 2 0 C) 1 2 2 D) 5 6 2 E) something else.

D

This program will not compile because it is missing something. What is it? #include <iostream> using namespace std; int main() cout << "Hello, world!"; return 0; } A) A comment on the first line, like //this is a program B) It needs <<endl before the ; of the cout statement C) It needs std:: before cout D) A ; after int main() E) A { at the beginning of the line after int main()

E

________ represent storage locations in the computer's memory.

Variables

A function is executed when it is: A) called B) defined C) described D) iterated E) verified

a

A(n) ______ produces a value that is passed to a function, and a(n) ______ is a variable that receives the value passed to the function. A) argument, parameter B) parameter, argument C) expression, local variable D) local variable, expression

a

Based on the code in main, what should go in the blank to make the prototype correct? _____ func (int); int main() { int x = 10; if (func(x)) cout << "You passed." << endl; } A) bool B) char C) void D) double E) Unable to tell without seeing the complete function definition.

a

Given the following function prototype void myFunction(double,string); which of the following statements are valid function calls to this function? A) myFunction(3.14,"Hello"); B) myFunction("Hello",3.14); C) myFunction() D) All of the above E) Only A and B are correct.

a

This type of variable is defined inside a function and is not accessible outside the function A) local variable B) global variable C) reference variable D) counter variable

a

What is output by the following code? void func1 (int x) { x=57; } int main { int m = 25; func1(m); cout << "m is " << m <<endl; } A) m is 25 B) m is 57 C) m is 58 D) Nothing is output

a

What is output by the following function when called with this function call: func1(10); ? void func1(int m) { cout << m << " "; if (m < 25) return; cout << m+1 << " "; } A) 10 B) 11 C) 10 11 D) Nothing it contains an error.

a

Which of the following C++ statements assign 3.4 to the x component of the start point of the firstLine variable? struct Point { float x, y; }; struct Line { Point start; Point end; }; Line firstLine; A) firstLine.start.x = 3.4; B) x.start.firstLline = 3.4; C) firstLine.x = 3.4; D) firstLine.Line.x = 3.4; E) None of the above

a

A function is: A) a named collection of statements. B) used to break programs into manageable units, or modules. C) Both A and B. D) None of the above.

c

Given the following declarations, which code below is valid and will copy the name and age from b into a? struct Person { string name; int age; }; Person a; Person b {"George", 10}; A) a = b; B) a.age = b.age; a.name = b.name; C) Both of the above. D) Neither of the above.

c

Given the program below, which variables could have the same name (without causing a compiler error)? void f (int a, int& b) { int c; c = a + b; } int g (int d) { int e; e = d*2; return e; } int h (int i) { return 2*i; } int main() { ... } A) a and b B) g and h C) c and e D) all of the above E) none of the above

c

The following statement defines a new ___________ called "Point". struct Point { double x; double y; }; A) variable B) function C) data type D) constant

c

The valid range of values for integer variable x is described by this relational expression: -10<=x && x<=10. Which of the following integer values could be used as a sentinel value when inputting a value for x in a loop? A) -1 B) 0 C) 50 D) There are no integers that could be used as a sentinel. E) Any integer value could be used as a sentinel.

c

What code should go in the blank in order to store a count of the numbers in the file in the count variable? int number, count=0; ifstream fin; fin.open("numbers.txt"); while (__________________) { count++; } A) count != -1 B) fin C) fin >> number D) number != -1

c

What does the following code do when executed? for (int i=1; i<=10; i++) for (int j=1; j<=4; j++) cout << "*"; A) Outputs a square with 10 lines of 4 stars each. B) Outputs a square with 4 lines of 10 stars each. C) Outputs 40 stars all on one line D) Outputs 40 stars, one per line, on 40 total lines.

c

What is output when the following program is executed? void function1 () { cout << "Function 1 "; } int main () { cout << "Main "; function1 (); } A) Main B) Function 1 C) Main Function 1 D) Function 1 Main E) Function 1 Main Function 1

c

What is the output of the following program? void doSomething(int num) { num = num + 1; cout << num << " "; } int main () { int x = 3; cout << x << " "; doSomething(x); cout << x << endl; } A) 3 4 5 B) 3 4 4 C) 3 4 3 D) 3 3 3

c

What is the output of the following program? void f(char ch) { cout << ch; } void pl(int count) { for (int i = 0; i < count; i++) f('+'); } int main() { int x = 2; pl(3); f('*'); pl(x); } A) +*+ B) +++*x C) +++*++ D) *+++++ E) None of the above

c

What is the output of the following program? void sequence(int count, char ch) { for (int i=0; i < count; i++) cout << ch; } int main() { int x = 2; char y = '!' sequence(4,'+'); sequence(x,y); } A) 4+2! B) ++++!!!! C) ++++!! D) ++!!!! E) 6!

c

What is this? void power(int,int); A) A function definition. B) A function call. C) A function prototype. D) All of the above.

c

What two elements are needed to calculate the total of a series of numbers? A) A loop and a variable that counts the numbers B) A loop and a variable that accumulates the total C) Two variables, one to count and one to accumulate the total D) Two loops, one nested inside of the other. E)

c

What will the following code display when executed? int numbers[5] = {99, 87, 66, 55, 101}; cout << numbers[2] << endl; A) 99 87 B) 87 C) 66 D) 55 E) Nothing, it has an error

c

What goes in the blanks to make this for loop sum the values 1..n? int n, x = 0; cout << "Enter a positive number: "; cin >> n; for (int count = 1; __________; count++) _______________ cout << x << endl; A) count <= n and x = x+count; B) count <=n and x+count; C) count < n and x = x+count; D) count < n and x+count; E) More than one of the above will work correctly. F) None of the above will work correctly.

A

What is displayed on the screen when the following code is executed? int number = 7; cout << "number" << number << endl; A) number7 B) numbernumber C) 77 D) 7number E) None of the above.

A

What is output by the following code? int main() { int x = 10; int y = 77; if (y > 50) { int x = 400; } cout << x << endl; } A) 10 B) 400 C) Nothing, you cannot have two variables with the same name.

A

Assuming all of the variables have been already declared, which of the following are valid assignment statements? (select all that are valid) A) total = 0; B) 72 = amount; C) x = y; D) 100 = 100;

A and C

This translates each source code instruction into the appropriate machine language instruction

A compiler

A set of well-defined steps for performing a task or solving a problem is known as:

Algorithm

Fill in the blank with the correct relational expression so that the message is output when integer variable x contains a value in this range: 3,4,5,6,7,8. if ( _________ ) cout << "The value is in range!" << endl; A) 3 <= x <= 8 B) 3 <= x && x <=8 C) x == 3 || 4 || 5 || 6 || 7 || 8 D) 3<=x || x<=8 E) More than one of the above will work correctly.

B

Given the following declaration, which of the statements below correctly assigns a literal value to the variable? string name; A) name = Jane; B) name = "Jane"; C) name = 'Jane'; D) name = (Jane);

B

What do each of the following expressions evaluate to? int x = 5; y = 6; 1. x == 5 2. 7 <= (x + 2) 3. (1+x) != y A) 1. true 2. true 3. true B) 1. true 2. true 3. false C) 1. true 2. false 3. true D) 1. true 2. false 3. false E) 1. false 2. true 3. true F) 1. false 2. true 3. false G) 1. false 2. false 3. true H) 1. false 2. false 3. false

B

What is output by the following code? cout << 9/4 << " "; cout << 9/4.0 << " "; cout << 9%4 << endl; A) 2.25 2.25 1 B) 2 2.25 1 C) 2.25 2.25 0.25 D) 2 2.5 0.25 E) None of the above is the correct output.

B

What is output by the following statements? float x = 12.3; cout << "!" << setw(5) << x; A) !12.3 B) ! 12.3 (there is 1 space between ! and 1) C) ! 12.3 (there are 5 spaces between ! and 1) D) !setw(5)12.3 E) None of the above

B

What is output when the following code is executed? int x = 7; do { cout << "Repeat " ; x = x + 1; } while (x <= 5); cout << "Done!" << endl; A) Done! B) Repeat Done! C) Repeat Repeat Repeat Repeat Repeat Done! D) Something else

B

What is the output of the following code, given the following contents of data.txt? ifstream inFile; inFile.open("data.txt"); int a, b; inFile >> a; cout << a << " "; inFile >> a >> b; cout << a << " " << b << endl; inFile.close(); A) 24 34 100 B) 24 13 34 C) 24 24 13 D) 24 13 E) 34 100

B

Given the following code, what value will be stored in j after this code is executed? int j = 12; double d = 45.90; j = d; A) 12 B) 45.90 C) 45 D) 46 E) Nothing, it is an error

C

Given the following declarations, I want to store x divided by y in a float variable z. Which of the answers below will keep the fractional part? int x; int y; A) float z = x/y; B) float z = static_cast<float>(x/y); C) float z = static_cast<float>(x)/y; D) More than one of the above will keep the fractional part. E) None of the above will keep the fractional part.

C

Given variable declarations: int x; string y; which statements will input 9 into x and Nick Foles into y if the user types the following when the program is running? 9 Nick Foles A) cin >> x; cin >> y; B) cin >> x; getline(cin,y); C) cin >> x; cin >> ws; getline(cin,y); D) More than one of the above will work. E) None of the above will work.

C

How many times will the following loop display "Hello"? for (int i=0; i<=20; i++) cout << "Hello!" << endl; A) 19 B) 20 C) 21 D) None E) Infinite

C

What data type can store the largest number? A) int B) float C) double D) char E) bool

C

What is displayed to the screen when the following code is executed? int main() { /* cout << "Be careful "; //cout << "This might be a trick "; cout << "question."; */ } A) Be careful This might be a trick question. B) Be careful question. C) There is no output.

C

What is output by the following statements? float a = 12.3, b = 6.789, c = 5; cout << fixed << setprecision(2) ; cout << a << " " << b << " " << c << endl; A) 12.3 6.789 5 B) 12.30 6.79 5 C) 12.30 6.79 5.00 D) 12 6.8 5.0 E) None of the above

C

Which of the following suggested changes will fix the error(s) in this code? double num1, num2; cout << "Please enter two numbers: "; cin >> num1 >> num2; if (num2 == 0) cout << "Division by zero is not allowed. " << endl; cout << "Please run the program again. " << endl; else cout << "The result is: " << num1/num2; A) use = instead of == B) put a semi-colon after: if (num2==0) C) put { before line 5 and } after line 6 D) More than one of the above will fix the errors. E) There are no errors in this code.

C

What is output when the following code is executed? int x = 0; while (x <= 5) { cout << x << "!"; x = x + 2; } A) 0!1!2!3!4!5! B) 0!1!2!3!4! C) 1!2!3!4!5! D) 0!2!4! E) 2!4!6! F) 1!3!5!

D

What is output when the following code is executed? int x = 1; while (x <= 5) cout << "Repeat " ; x = x + 1; cout << "Done!" << endl; A) Done! B) Repeat Done! C) Repeat Repeat Repeat Repeat Repeat Done! D) Something else

D

What is output when the following code is executed? int x = 1; while (x <= 5) { cout << "Repeat " ; } cout << "Done!" << endl; A) Done! B) Repeat Done! C) Repeat Repeat Repeat Repeat Repeat Done! D) Something else

D

What is the output of the following code segment if the user enters 90 for the score? cout << "Enter your test score: "; cin >> test_score; if (test_score >= 90) cout << "You got an A."; if (test_score >= 60) cout << "You passed the test." else cout << "You need to study harder." ; A) You got an A. B) You passed the test. C) You need to study harder. D) You got an A.You passed the test. E) None of the above

D

What is wrong with the following switch statement, assuming tempt is an int variable? switch (temp) { case temp < 0 : cout << "Temp is negative.\n"; break; case temp == 0: cout << "Temp is zero.\n"; break; case temp > 0 : cout << "Temp is positive.\n"; break; } A) Nothing is wrong with this statement. B) You need braces { } around each cout statement. C) It will not compile because it is not indented properly. D) A relational expression like "temp==0" is not allowed after "case". E) A default: case is required.

D

What will be output when the following code is executed? for (int x=1; x <= 5; x++) { cout << "One "; } cout << "Done!" << endl; A) Done! B) One Done! C) One One One One Done! D) One One One One One Done! E) One One One One One One Done!

D

Which C++ data type should be used to declare a varible to store a real number (one with digits after the decimal point) such as 3.14159? A) int B) string C) bool D) float

D

Which C++ statement below performs the following algebraic expression? A) g = h + 12 / 4k; B) g = h + 12 / 4*k; C) g = (h + 12) / 4k; D) g = (h + 12) / (4*k); E) More than one of the above will work.

D

Which of the following statements correctly inputs a value from the user into variable x? A) cin >> "Please enter a number: " >> x; B) cout << "Please enter a number: " >> x; C) cout >> "Please enter a number: "; cin << x; D) cout << "Please enter a number: "; cin >> x; E) None of the above will work.

D

What do the following expressions evaluate to? 1. true && false 2. true || false 3. !(5==5) A) 1. true 2. true 3. true B) 1. true 2. true 3. false C) 1. true 2. false 3. true D) 1. true 2. false 3. false E) 1. false 2. true 3. true F) 1. false 2. true 3. false G) 1. false 2. false 3. true H) 1. false 2. false 3. false

F

Which change(s) below will fix the compiler error(s) (shown in the comments) in the following code? void showArray (int nums) { for (int i=0; i<5; i++) cout << nums[i] << " "; //error: nums is not an array, pointer, or vector cout << endl; } int main() { int a[5] = {11, 12, 13, 14, 15}; showArray (a); //error: no matching function for call to 'showArray' } A) Line 1: (int nums) => (int nums, int size) B) Line 1: (int nums) => (int nums[ ]) C) Line 8: showArray (a); => showArray(a[5]); D) All of the above E) Both B and C

b

Which of the following statements outputs the value of the gpa member of the second structure of the student array? A) cout << student.gpa[1]; B) cout << student[1].gpa; C) cout << student.gpa; D) cout << gpa.student[1];

b

A special value that marks the end of a list of values is a A) constant B) counter C) variable D) sentinel E) None of these

d

What is output by the following program? const int SIZE = 5; void sky(int a[ ]) { a[1] = 25; a[SIZE-1] = 66; } int main() { int nums[SIZE] = {1,2,3,4,5}; sky(nums); for (int i=0; i<SIZE; i++) cout << nums[i] << " "; cout << endl; } A) 1 2 3 4 5 B) 25 2 3 66 25 C) 1 25 3 66 5 D) 1 25 3 4 66 E) 25 2 3 4 66

d

What will be output by the following code? int numbers[] = {99, 87, 66, 55, 101}; for (int i=1; i < 4; i++) cout << numbers[i] << " "; A) 99 87 66 55 B) 87 66 55 101 C) 99 87 66 D) 87 66 55 E) Nothing, this code has an error.

d

What will fix the error in the following code, which copies the elements from arrayB to arrayA? int arrayA[3]; int arrayB[3] = {3,6,9}; arrayA = arrayB; A) There is nothing wrong with this code, it will work correctly. B) arrayA must be initialized first. C) arrayA=arrayB should be arrayA[i] = arrayB[i]. D) arrayA=arrayB should be arrayA[0]=arrayB[0]; arrayA[1]=arrayB[1]; arrayA[2]=arrayB[2]; E) arrayA=arrayB should be arrayB=arrayA.

d

Given the following function definition void f(int x) { cout << x*2 << endl; } which of the following statements are valid function calls to this function? A) f( ); B) f(1); C) f("one"); D) f(x); //where x is a variable of type int E) both B and D are correct.

e

Given the following function definition: double half (double a) { return a/2; } which of the following is not a valid statement in main? Assume main contains: double x; A) cout << half(x); B) double w = half(x+100); C) double t = (37 * half(x)) / 3000; D) double u = half(half(x)); E) All of the above are valid.

e

Given the program below, which line causes a compiler error? int x; // 1 void f (int a) { int c; // 2 c = a + x; // 3 } int g (int d) { int x; // 4 x = d*2; return x; } int main() { x = 10; // 5 c = 25; // 6 } A) // 1 B) // 2 C) // 3 D) // 4 E) // 5 F) // 6

f

Every complete C++ program must have a

function main


Related study sets

Structure & Stereochemistry of Alkanes (Ch. 3)

View Set

Part 3- Saunders NCLEX Cardiovascular

View Set

Chapter 1: Nursing Past & Present

View Set

3.5 Average revenue, total revenue and profit.

View Set

Chapter 7 and 8 Criminal Justice

View Set