CSC 101 final

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

int x, y; double z; cin >> x >> y >> z; input: 37 86.56 32

x= 37 y= 86 z=.56

cin >> sum; cin >> n; for (int j = 1; j<4; j++){ cin >> n; if (n == -1) break; sum += n; } cout <<"sum= " <<sum; input: 45 38 71 4 -1

120

What is the output of the following code? int numbers[] = {1,2,3}; for(int i = 0; i < 3; ++i) { cout << numbers[i]; }

123

Evaluate the following: (17%5+4)*18/9+5+3*(8/(14%10)-3)

14

What is the result of this. input: 18.7ab%12 int i; float f; cahr ch; cin >> i; cin >> f; cin >> ch; i = ? f = ? ch ?

18 0.7 a

You were asked to write a program to add two numbers. Instead, you write a program that multiplies the two numbers. What type of error is this?

semeantic

bool isAdult = true; if(isAdult) { cout << "Adult"; } else { cout << "Child"; } What is the output?

Adult

int main(){ int occurrences[5] = {3, 2, 3, 1, 5}; int total[5] = {15, 2, 9, 5, 10}; for(int i = 0; i < 5; ++i){ cout << "Average " << i+1 << " = " << total[i]/occurrences[i] << endl; } return 0; }

Average 1 = 5 Average 2 = 1 Average 3 = 3 Average 4 = 5 Average 5 = 2

How many times will "!" print? int i = -5 while(-5 <= i <= 0) { cout << "!"; --i; }

0

Fill in the blank to check if the file is open. If the file is not open ensure that the program prints "File not open" and exits. What should be put in expression? #include <iostream> #include <fstream> using namespace std; int main() { ifstream inf; inf.open("input.txt"); if(____[expression]___) { cout << "File not open"; return 0; } return 0; }

!inf

what is the output of this int s =0; for (int i = 0; i < 5; i++){ s = 2*s-i; } cout << s;

-26

How many times will "!" print? int i = -5 while(-5 <= i <= 0) { cout << "!"; --i; }

0

What would the output of the following code be? enum drinks {DrPepper, Coke, Sprite}; drinks myOrder = DrPepper; cout << myOrder;

0

What would the output of the following code be? for(int i = 0; i < 5; ++i) { for(int j = 0; j < i; ++j) { cout << j; } }

0010120123

What is the ouput of the following code? void loop(int num) { for(int i = 1; i < num; ++i) { for(int j = 0; j < 5; ++j) { cout << j; } } } int main() { loop(3); return 0; }

0123401234

Evaluate the following expression: 5 % 2 = ?

1

Given the string: string s1 = "Halloween"; what would be the return value be of the following function call: s1.find("all");

1

How many times will '$' be printed? int i = 6; do { cout << "$"; ++i; } while(i < 5);

1

How many times will '$' be printed? int i = 6; do { cout << "$"; ++i; } while (i < 5)

1

enum bowTypes { RECURVE, COMPOUND, LONG, CROSS } int main(){ bowTypes bow = LONG; bow = static_cast<bowTypes>(bow-2); cout << bow << endl; return 0; }

1

for(int i =1; i<5;i++){ cout << i << " "; for(int j = 1; j <= i; j++){ cout << j << " "; } cout << endl; }

1 1 2 1 2 3 1 2 3 4 1 2 3 4

int main(){ int nums[5] = {1, 2, 3, 4, 5}; for(int i = 0; i < 5; ++i){ if(nums[i]%2 != 0){ cout << nums[i] << " "; } } return 0; }

1 3 5

What is the output of the following code? int myFunc(int n) { n = 10; return n; } int main() { int n = 4; cout << myFunc(n); }

10

What is the output of the following code? void myFunc(int &n) { n = 10; } int main() { int num = 4; myFunc(num); cout << num; }

10

int main(){ char charArray[3] = {'A','B','C'}; int intArray[3] = {100, 200, 300}; cout << intArray[(static_cast<int>(charArray[0])-65)]; return 0; }

100

Given the following input: 12.34.a What would the output of the following be? int i; float j; char ch; cin >> i >> j >> ch cout << i << " " << j << " " << ch;

12 .34 .

What is the output of the following code? char symbol = '^'; int i = 10; int j = 1; int result = 0; switch(symbol) { case '+': result = i + j; break; case '-': result = i - j; break; default: result = 180; break; } cout << result;

180

What is the output of the following code? int numbers[3] = {1,2,3}; cout << numbers[1];

2

What would be output by this program? #include <iostream> using namespace std; int main() { float myNum = 0; myNum = 6 % 5 - 2; myNum = 10 / 4; cout << myNum; return 0; }

2

How many iterations are in the following "while" loop? int i = 0; while(i < 20) { ++i; }

20

enum color {RED, GREEN, BLUE}; int main(){ int nums[3] = {100, 200, 300}; color c1 = GREEN; switch(c1){ case RED: cout << nums[static_cast<int>(RED)]; break; case GREEN: cout << nums[static_cast<int>(GREEN)]; break; case BLUE: cout << nums[static_cast<int>(BLUE)]; break; default: cout << "Error"; } return 0; }

200

What is the output of the following program? string sent = "I am a long sentence that says the word a"; string word = "a"; cout << sent.find(word, 6);

23

How many times does "#" print? for(int i = 0; i < 4; ++i) { if(i == 2) { continue; } cout << "#"; }

3

What is the output of the following code? void myFunc(int n) { n = 10; } int main() { int num = 4; myFunc(num); cout << num; }

4

int main(){ int arr[5] = {0}; int arr2[5] = {0}; for(int i = 0; i < 5;++i){ arr[i] = 5-i; } for(int i = 0; i < 5;++i){ arr2[i] = i+1; } for(int i = 0; i < 5; ++i){ cout << arr[i] - arr2[i] << " "; } }

4 2 0 -2 -4

what is the output of this code int main(){ int a = 20; twice(a); cout << a << endl; return 0; } void twice(int x){ x = 2*x; x = 2*x; }

40

How many times will '+' be printed? int i = 0; while(true) { if(i == 5) { break; } cout << "+"; ++i; }

5

int main(){ int arr[5]; for(int i = 0; i < 5;++i){ arr[i] = 5-i; } for(int i = 0; i < 5; ++i){ cout << arr[i] << " "; } }

5 4 3 2 1

If the input stream contains: 5.12 and the following code runs #include<iostream> using namespace std; int main(){ int hoursWorked = 0; float payRate = 0; cin >> hoursWorked >> payRate; return 0; } What is stored in payRate at the end of this section of code?

5.12

How many '?' will be printed? for(int i = 0; i < 10; ++i) { for(int j = 0; j < 5; ++j) { cout << "?"; } }

50

How many '?' will be printed? for(int i = 0; i < 10; ++i) { for(int j = 0; j < 5; ++j) { cout << "?"; } }

50

How many iterations are in the loop? int i = 0; while(i < 50) { ++i; }

50

How many iterations are in the following "for" loop? for(int i = 5; i < 17; i = i + 2) { // Work }

6

How many iterations are in the loop? for(int i = 5; i < 17; i = i + 2) { // Work } 6

6

What is the output of the following program? string sent = "hello world"; cout << sent.find("w");

6

how many times will '+' be printed? int i = 0; while(true) { if(i == 7) { break; } cout << "+"; ++i; }

7

What is the output of the following code? int numbers[][3] = {{1,2,3},{4,5,6},{7,8,9}}; cout << numbers[2][1];

8

What is output by the following program? char symbol = '-'; int i = 10; int j = 1; int result = 0; switch(symbol) { case '+': result = i + j; break; case '-': result = i - j; break; default: result = 90; break; } cout << result;

9

n = 5; while (n <= 7){ n += 2; } cout < "n= " << n << endl;

9

What is the output of the following program? char symbol = '+'; int i = 10; int j = 1; int result = 0; switch(symbol) { case '+': result = i + j; case '-': result = i - j; default: result = 90; } cout << result;

90

What is the outcome of this program? int num = 0 for(int num = 0; ; ++ num) { if (num <= 67){ continue; } if(num == 10000){ break; } cout << "I have no idea"; ++num }

9931

What is the outcome of this program? int num = 0 for(int num = 0; ; ++ num) { if (num == 67){ continue; } if(num == 10000){ break; } cout << "I have no idea"; ++num }

9999

input.txt = 1 C 2 B 3 A int main(){ int nums[3] = {0}; char chars[3] = {'0'}; ifstream fin; fin.open("input.txt"); for(int i = 0; i < 3;++i){ fin >> nums[i] >> chars[i]; } for(int i = 2; i >= 0; --i){ cout << chars[i]; } cout << " as easy as "; for(int i = 0; i < 3; ++i){ cout << nums[i]; } fin.close(); return 0; }

ABC as easy as 123

(True or False) The following code will compile. int num[];

False

(True or False) The following code would not generate a runtime error and will output 12345. int numbers[] = {1,2,3,4,5}; for(int i = 0; i <= 5; ++i) { cout << numbers[i]; }

False

(True or False) The following function will compile. void print(int num) { return num+1; }

False

(True/False) The following code would compile. enum {Red, Blue, Green} colors; colors myFavColor;

False

Evaluate the following. (23 <= 10 ) || (12 != 5) && (!(45 == 43))

False

T or F. The following is a valid identifier: 2ndNum

False

True or False: The following code would compile without any errors: #include <iostream> using namespace std; int main() { int num = 0; cin << num; cout >> "num = " >> num >> endl; return 0; }

False

int main(){ string strArray[6] = {"Good", "luck", "on", "the", "last", "lab!"}; for(int i = 0; i < 6;++i){ cout << strArray[i] << " "; } return 0; }

Good luck on the last lab!

What is the output of the following code? bool sayHi = false; if(sayHi) cout << "HI!" << endl; cout << "How are you?" << endl; cout << "Done.";

How are you?\nDone.

Distinguish between a variable and a value

variable is a container; valuable is hard set

Fill in the blanks to replace every letter of the string with 'x' string Line; getline(inFile, Line); for(int i = 0; i < _________; i++){ ______= 'x'; }

Line.size() Line[ i ]

string strArray[6] = {"easy","hard","annoying","great","yet", "but"}; int main(){ cout << "This quiz is not " << strArray[1] << ", " << strArray[5] << " it is " << strArray[2]; return 0; }

This quiz is not hard, but it is annoying

(True or False) The following code will compile. int numbers[] = {1,2,3};

True

(True or False) The following code will output "I was true". bool isGreater(string s1, string s2) { if(s1 > s2) { return true; } return false; } int main() { if(isGreater("before","back")) { cout << "I was true"; } else { cout << "I was false"; } return 0; }

True

Is the following expressions true or false? Given int num = 20; Expression: (23 > num)

True

T or F. The following is a valid identifier: _myChar

True

T or F. The following is a valid identifier: myNumber

True

T or F: CPU can only access data in memory

True

int main() { bool flag = false; int num = 0; while(!flag); { cin >> num; if(num == -1) { flag = true; } } return 0; }

True

What would the output of the following code be? string s1 = "Hello"; string s2 = "World"; s1.swap(s2); cout << s1 << " " << s2;

World Hello

What character is at the end of a C-string? (Your answer should be the charecter itself not the name of it for example the newline character it would be \n)

\0

What are the parameters for a valid identifier?

any name containing letters, digits, and underscores. Punctuation and symbols are not valid

Fill in the following code to crash the program if the file fails to open #include <iostream> #include <fstream> #include < ______________ > using namespace std; int main() { ifstream inf("input.txt"); if(!inf) { cout << "Failed to open file" << endl; _______________ ( ___________ ) // type either true of false in the parenthesis which ever one would make the program crash } return 0; }

cassert assert false

T or F: Arrays in C++ can be passed to functions by value and passed to functions by reference.

false

T or F: the following is not an infinite -loop for ( int i = 12; i <= 25; i--) cout << i;

false

T or F: Function g is correctly overloaded. So, it will compile int g(int a, int b); void g(int a, int b);

false. To properly overload a function the types have to vary to some degree. Example being changing void g(int a, float b);

Fill in the following blanks to preform file output: #include<iostream> #include< ____________ > using namespace std; int main() { _________________ outFile; outFile.________ ("input.txt"); outFile ____ "Hello World"; return 0; }

fstream ofstream open <<

What is the output of the following code? string text = "hello"; for(int i = 0; i < text.size(); ++i) { cout << text[i] << ","; }

h,e,l,l,o,

How would you call the function isEven()? namespace helper { bool isEven() { return true; } }

helper::isEven(); helper::isEven()

What would the following code output, if it won't compile then put "error" as the answer? enum {NewOrleans, NewYork, Tokyo} places; string str = "This is the string in question"; cout << str[Tokyo];

i

What is the result of this code? int n = -10, counter = 0; int sum = 0; while (n>100 || counter < 5){ sum += n ; n += 10; } cout << "sum = " << sum << endl;

infinite loop

If you wanted to make a typedef called value_type that was an int what would go in the blanks below? typedef ______ _________________;

int value_type

You are given a 2D array named array2D which holds values of type int. The dimensions of of the array ROWS and COLUMNS. write code to output the max value found in the array.

int array 2D[ ] = { 0 };

What must you include to do formatted input and output #include<[answer]>

iomanip

what is the output void f(int a){ while(a--){ static int n =0; int x = 0; cout << "n: " < n++ << " ,x:" << x++ << endl; } } int main(){ f(3); return 0; }

n: 0, x: 0 n: 1, x: 0 n: 1, x: 0

Will this compile? void print_odd(int a){ if(a % 2 == 0) return; cout << a << " is odd" << endl; } int main(){ print_odd(35) int z = print_odd(21); return 0; }

no because you cant assign a void function

Given the following input: a234 What is the output to the program: #include<iostream> using namespace std; int main() { int num = 0; char myChar = 'z'; cin >> num; cin >> myChar; return 0; } What are the values stored in the variables?

num = 0 myChar = z

What is the output of the following program? int num = -10; if(0 < num < 100) { cout << "num is within the range" << endl; } else { cout << "num is outside the range" << endl; }

num is outside the range

The code below is given the following input: 125a5b458 #inlcude<iostream> using namespace std; int main() { int num1 = 0; int num2 = 0; char myChar = ' '; cin >> num1; cin.get(myChar); cin.ignore(3, '\n'); cin >> num2; return 0; } What are the value of the variables?

num1 = 125 num2 = 58 myChar = a

What is the output of the following code? int grade = 70 string result = (grade > 60) ? "failing" : "passing"; cout << result;

passing

Write code using a string member function to find the second occurrence of section in sentence. string section = "Fight Club"; string sentence = "The first rule of Fight Club is: Don't talk about Fight Club."; int A = _______________; cout << A << endl;

sentence.index(section, 18);

Evaluate the following. (23 <= 10 ) || (12 != 5) && (!(45 == 43))

true

T or F: Data in memory is lost once the power is turned off

true

T or F: The following code prints out a grade only when score is equal to or greater than 60. if (score >= 60) grade = 'P'; cout << "The grade is: " << grade << endl;

true

T or F: c++ is case sensitive

true

T or F: the following while loop will never terminate. int i = 0; while (i <= 0);{ i = i + 5; cout << i << " "; }

true


Conjuntos de estudio relacionados

Texas Law of Contracts - Chp. 9 Conveyance of Title

View Set

General Intro To Cloud Computing

View Set

A Night to Remember *Coach Masson

View Set

PrepU: Chapter 12: Nursing Management During Pregnancy

View Set

SIE 557 Midterm Review Questions

View Set

Chapter 01 Limits, Alternatives, and Choices

View Set

CH: 24 Nursing Care of the Newborn and Family ( Louder milk)

View Set