All the Programming 1 Quizzes thrown into one set
Which one of the following code snippets compiles without errors and displays the output "Hello world" on the screen?
#include <iostream> using namespace std; int main() { cout << "Hello world" << endl; return 0; }
Which one of the following code snippets compiles without errors and displays the output "hello" on the screen?
#include <iostream> using namespace std; int main() { cout << "hello" << endl; return 0; }
Which one of the following operators computes the remainder of an integer division?
%
Which operator is used to concatenate two or more strings?
+
Which of the following operators is NOT a relational operator?
+=
What is the output of the following code snippet? #include <iostream> #include <string> using namespace std; int main() { int num1 = 10; int num2 = 5; int num3 = 200; num3 = num3 % (num1 * num2); cout << num3 << endl; return 0; }
0
What is the output if the function call is testmyval(6) in the following code snippet? void testmyval(int nval) { if (nval > 0) { testmyval(nval - 2); } cout << nval << " "; }
0 2 4 6
What is the output of the code snippet given below? int i = 0; while (i != 11) { cout << i <<""; i = i + 2; }
0 2 4 6 8 10 12 14 ... (infinite loop)
What is the output of the code snippet given below? int i = 0; while (i != 9) { cout << i << " "; i = i + 2; }
0 2 4 6 8 10 12 14 ... (infinite loop)
Consider the following code snippet: int ctr = 0; int myarray[3]; for (int i = 0; i < 3; i++) { myarray[i] = ctr; ctr = ctr + i; } cout << myarray[2]; What is the output of the code snippet?
1
Consider the following code snippet: vector<double> somedata; somedata.push_back(10); What is the size of the vector somedata after the given code snippet is executed?
1
Consider the following code snippet: vector<int> num(3); num[0] = 1; num[1] = 2; num[2] = 1; int cnt = 0; for (int i = 0; i < num.size(); i++) { if (num[i] % 2 == 0) { cnt++; } } What is the value of the cnt variable after the execution of the given code snippet?
1
What is the output of the following code snippet? int my_fun(int perfect) { return ((perfect -1) * (perfect -1)); } int main () { for (int i = 0; i < 4; i++) { cout << my_fun(i) << " "; } return 0; }
1 0 1 4
What is the output of the following code snippet? int i = 1; while (i < 20) { cout << i <<" "; i = i + 2; if (i == 15) { i = 19; } }
1 3 5 7 9 11 13 19
What is the output of the following code snippet? int i = 1; while (i < 10) { cout << i << " "; i = i + 2; if (i == 5) { i = 9; } }
1 3 9
What is the output of the following code snippet? int i = 1; while (i != 9) { cout << i << " "; i++; if (i = 9) { cout << "End"; } }
1 End
Evaluate the given pseudocode to calculate the payment (pmt) with the following test values: The total number of hours worked (working_hours) = 60 The rate paid for hourly work (rate) = 15 input working_hours input rate pmt = working_hours * rate if working_hours > 40 then extra_hours = working_hours - 40 extra_pmt = extra_hours * rate * 2 pmt = pmt + extra_pmt end of if output pmt
1,500
The two strings "Aardvark" and "Aardvandermeer" are exactly the same up to the first six letters. What is their correct lexicographical ordering?
"Aardvandermeer" is first, then "Aardvark"
What is the output of the following code snippet? int i = 1; while (i <= 10) { cout << "Inside the while loop" << endl; i = i * 11; }
"Inside the while loop" will be displayed only once.
Assuming that a user enters 10, 20, and 30 as input values one after another, separated by spaces, what is the output of the following code snippet? int main() { int num1; int num2; int num3 = 0; cout << "Enter a number: "; cin >> num1; cout << "Enter a number: "; cin >> num2; cout << "Enter a number: "; cin >> num3; if (num1 > num2) { if (num1 > num3) { cout << num1 << endl; } else { cout << num3 <<endl; } } else { if (num2 > num3) { cout << num2 << endl; } else { cout << num3 << endl; } } return 0; }
30
How many elements can be stored in an array of dimension 5 by 6?
30
Consider the following code snippet: int myarray[] = { 10, 20, 30, 40, 50 }; cout << myarray[2]; cout << myarray[3]; What is the output of the code snippet?
3040
What is the output of the following code snippet? int pow(int base, int power) { int result = 1; for (int i = 0; i < power; i++) { result = result * base; } return result; } int main() { cout << pow(pow(2, 2), 2) << endl; return 0; }
16
How many times will cout statement execute in the following code snippet? for (int num2 = 1; num2 <= 4; num2++) { for (int num1 = 0; num1 <= 3; num1++) { cout << num2 << " " << num1 << endl; } }
16 times
Consider the following code snippet: const int SIZE = 5; int data_array[SIZE]; for (int i = 0; i < SIZE; i++) { data_array[i] = 2 * (i-1); } What value is stored in position 2 of the array?
2
How many constructors are there in the following class declaration? class CashRegister { public: CashRegister(); CashRegister(int count); void set_item_count(int count); void view() const; private: int item_count; }; CashRegister::CashRegister() { set_item_count(0); } CashRegister::CashRegister(int count) { set_item_count(count); }
2
What is the output of the following code snippet? int absval(int a) { if (a < 0) { return -a; } else { return a; } } int main() { cout << absval(-2); return 0; }
2
How many times does the following loop run? int i = 0; int j = 1; do { cout << i << ";" << j << endl; i++; if (i % 2 == 0) { j--; } } while (j >= 1);
2 times
Consider the following code snippet: int size1 = 0; int size2 = 0; int num1[20]; int num2[10]; for (int cnt = 0; cnt < 5; cnt = cnt++) { num1[cnt] = cnt + 1; size1++; if (size2 < 4) { num2[cnt] = cnt + 1; size2++; } } What is the value of the size2 variable after executing the given code snippet?
4
What is the output of the following code snippet? #include <iostream> using namespace std; int main() { int value = 3; value++; cout << value << endl; return 0; }
4
What is the output of the following code snippet? vector<int> num(3); num.push_back(3); cout << num.size();
4
Assuming that the user enters 23 and 45 as inputs for num1 and num2, respectively, what is the output of the following code snippet? #include <iostream> #include <string> using namespace std; int main() { cout << "Enter a number: "; string num1; cin >> num1; cout << "Enter another number: "; string num2; cin >> num2; string final = num1 + num2; cout << final << endl; return 0; }
2345
Consider the following code snippet: int mynum[5]; for (int i = 1; i < 5; i++) { mynum[i] = i + 1; cout << mynum[i]; } What is the output of the given code snippet?
2345
What is the output of this code snippet? int sum = 22; sum = sum + 2; cout << sum++; // sum = sum + 4; cout << sum;
2425
Consider the following code snippet: vector<int> vectarr(5); for (int i = 0; i < vectarr.size(); i++) { vectarr[i] = i + 3; } What value is stored in the 0th element of the vector?
3
What is result of evaluating the following expression? 1 + 2 * 3 - 4
3
How many times does the following loop run? int i = 0; int j = 1; do { cout << i << ";" << j << endl; i++; if (i % 3 == 0) { j--; } } while (j >= 1);
3 times
What is the output of the following code snippet? int check(vector<int> vectdata) { int sum = 0; for (int i = 0; i < vectdata.size(); i++) { sum = sum + vectdata[i]; } return sum; } int main() { vector<int> vdata(3); int rsum; for (int cnt = 0; cnt < 3; cnt++) { vdata.push_back(cnt + 1); } rsum = check(vdata); cout << rsum; return 0; }
6
Evaluate the given pseudocode to calculate the payment (pmt) with the following test values: The total number of hours worked (working_hours) = 50 The rate paid for hourly work (rate) = 10 input working_hours input rate pmt = working_hours * rate if working_hours > 45 then extra_hours = working_hours - 45 extra_pmt = extra_hours * rate * 2 pmt = pmt + extra_pmt end if output pmt Select one:
600
What is the output of the following code snippet? int black_box(int a) { int val; if (a <= 0) { val = 1; } else { val = a + black_box(a - 2); } return val; } int main() { cout << black_box(4); return 0; }
7
What is the output of the following code snippet? int main() { double a; a = sqrt(9.0) + sqrt(16.0); cout << a << endl; return 0; }
7.0
What is the value of pow(2, 3)?
8
How many times will the following loop run? int i = 0; while (i < 9) { cout << i << endl; i++; }
9
string name = "Joan Hunt"; cout << name.length();
9
Consider the following code snippet: int arrmarks[5]; for (int cnt = 0; cnt < 5; cnt++) { cout << "Enter the marks: "; cin >> arrmarks[cnt]; } Assume that a user enters 35, 56, 78, 90, and 45 as marks. What is stored in the element with the index number 3?
90
Which of the following operators is used as a relational operator?
<=
Assuming that a user enters 45, 78, and 12 one after another, separated by spaces, what is the output of the following code snippet? int main() { int num1; int num2; int num3 = 0; cout << "Enter a number: "; cin >> num1; cout << "Enter a number: "; cin >> num2; cout << "Enter a number: "; cin >> num3; if (!(num1 > num2 && num1 > num3)) { cout << num1 << endl; } else if (!(num2 > num1 && num2 > num3)) { cout << num2 << endl; } else if (!(num3 > num1 && num3 > num2)) { cout << num3 << endl; } return 0; }
45
Assuming that the user enters 45 and 62 as inputs for n1 and n2, respectively, what is the output of the following code snippet? #include <iostream> #include <string> using namespace std; int main() { cout << "Enter a number: "; string n1; cin >> n1; cout << "Enter another number: "; string n2; cin >> n2; string result = n1 + n2; cout << result << endl; return 0; }
4562
What is the output of the following code snippet? #include <iostream> using namespace std; int main() { int value = 25; value = value * 2; value--; cout << value << endl; return 0; }
49
How many times will the following loop run? int i = 0; while (i < 5) { cout << i << endl; i++; }
5
How many times does the following loop execute? double num; double sum = 0; for (int i = 0; i < 10; i++) { cout << "Please enter a number: "; cin >> num; if (sum > num) { sum = num; } if (i == 3) { i = 8; } }
5 times
How many elements can be stored in an array of dimension 2 by 3?
6
What name do you use for small computers that are programmed to control automobile engines and cell phones?
Embedded systems
What is the output of the following code snippet? vector<int> num(4); num.push_back(4); cout << num.length();
Error, won't compile
In a switch statement, if a break statement is missing
Execution falls through the next branch until a break statement is reached
What is the output of the following code snippet? string firstname = "William"; string lastname; cout << "First: " << firstname << endl; cout << "Last: " << lastname << endl;
First: William Last:
Assuming that the user inputs "Joel" at the prompt, what is the output of the following code snippet? #include <iostream> #include <string> using namespace std; int main() { cout << "Enter your name "; string name; cin >> name; name += ", Good morning"; cout << name << endl; return 0; }
Joel, Good morning
Consider the following code snippet: int number[3]; // Line 1 number[3] = 5; // Line 2 Which one of the following statements is correct for the given code snippet?
Line 2 causes a bounds error because 3 is an invalid index number.
Which one of the following programs combines the machine code with the library code to build an executable file?
Linker
What kind of error is created by the following code snippet? cout << "The sum of 8 and 12 is " << 8 * 12 << endl;
Logic error: the program does not produce the desired result
The following code snippet contains an error. What is the error? int cost = 0; cin >> cost; if (cost > 100); { cost = cost - 10; } cout << "Discount cost: " << cost << endl;
Logical error: if statement has do-nothing statement after if condition
One advantage of designing functions as black boxes is that
Many programmers can work on the same project without knowing the internal implementation details of functions
Which of the following are NOT types of code used by an IDE?
Native code
An if statement inside another if statement is called a
Nested if statement
In a car rental application, the following Car class interface is defined. One of the programmers discovers a bug in the void accelerate(double speed) function. She corrects the bug by modifying the code snippet in the implementation of the Car class accelerate function. What must be changed in the interface? class Car { public: void start(); void accelerate(double speed); void stop(); double get_speed() const; private: double speed; };
No change is required in the interface because only the implementation was modified.
Is there any error in the following code snippet, which is used for calculating the average age for a group of three students? #include <iostream> int main() { int age1 = 15; int age2 = 18; int age3 = 24; int average = (age1 + age2 + age3) / 3; cout << "The average is " << average; return 0; }
No error; the code snippet is completely accurate.
What is the output of the following code snippet? bool token = false; while (token) { cout << "Hello World!" << endl; }
No output
Is the code snippet written below legal? string s = "1234"; for (int i = 0; i <= 4; i++) { cout << s.substr (i, 1); }
No, there will be a string index out of bounds error.
What is the output of the following code snippet? int main() { int digit = 500; if (digit != 500) { cout << "500" << endl; } else { cout << "Not 500" << endl; } return 0; }
Not 500
Consider the following statements about folders and your integrated development environment (IDE): I. Hierarchical folders help to organize a project II. Folders are a way to visualize the layout of a file system III. Folders make it impossible to lose or accidentally delete a file
Only I and II are correct
Consider the following division statements: I. 22 / 7 II. 22.0 / 7 III. 22 / 7.0 IV. 22.0 / 7.0 Which of the following options is correct?
Only I will return an integer value.
Consider the following C++ variable names: I. 1st_instance II. basic_in_$ III. _emp_name_ IV. address_line1 V. DISCOUNT Which of the following options is correct?
Only III, IV, and V are valid C++ variable names.
How many times does the following code snippet display "Loop Execution"? for (int i = 0; i < 10; i++); { cout << "Loop Execution" << endl; }
Only one time.
The term "Black Box" is used with functions because
Only the specification matters; the implementation is not important.
A single silicon chip made from potentially millions of transistors is called
A Central Processing Unit (CPU)
What are the two parts of an if statement?
A condition and a body
Which of the following is true about function return statements?
A function can hold multiple return statements, but only one return statement executes in one function call.
A loop inside another loop is called:
A nested loop
What are the following public functions in the listed Car class definition known as? class Car { public: void start(); void accelerate(double speed); void stop(); double get_speed() const; private: double speed; bool check_speed() const; };
A public interface
Consider the function shown below. int do_it(int x, int y) { int val = 0; if (x < y) { val = x + 1; } else { val = y - 1; } // the last statement goes here } What should be the last statement of this function?
A return statement
What type of error can you identify in the following code snippet? #include <iostream> int main { int a = 10; int b = 20; int c = a + b; return 0; }
A syntax error.
What will be the output of the following code snippet? int i; int j; for (i = 0; i < 6; i++) { for (j = 7; j > i; j--) { cout << "+"; } cout << endl; }
An inverted right triangle with six rows and seven columns of the plus sign. The number of columns decrements by one on completion of one iteration of the inner loop.
High level programming languages
Are independent of the underlying hardware
In the following code snippet, which constructor is called for the object declaration CashRegister reg(5)? class CashRegister { public: CashRegister(); CashRegister(int count); void set_item_count(int count); void view() const; private: int item_count; }; CashRegister::CashRegister() { set_item_count(0); } CashRegister::CashRegister(int count) { set_item_count(count); } int main() { CashRegister reg(5); return 0; }
CashRegister(int count)
What kind of error is it when your program has a syntax error?
Compile-time error
Which one of the following translates high-level descriptions into machine code?
Compiler
In an accounting application, you discover several places where the total profit, a double value, is calculated. Which of the following should be done to improve the program design?
Consider writing a function that returns the total profit as a double value.
For the given code snippet, which of the following statements is true? double raise(double rate) { double new_pay_rate = rate * 1.1; return new_pay_rate; } int main() { double rate = 40.0; double new_pay_rate = 0.0; new_pay_rate = raise(rate); cout << "Pay rate: " << new_pay_rate << endl; return 0; }
The code snippet executes and displays "Pay rate: 44.0".
Consider the following code snippet: int arrmarks[5]; int total; for (int cnt = 1; cnt <= 5; cnt++) { cout << "Enter the marks: "; cin >> arrmarks[cnt]; total = total + arrmarks[cnt]; } cout << total; Which one of the following outputs is displayed on executing the given code snippet?
The code snippet has a bounds error or displays redundant data.
Consider the following code snippet: int marks[3] = { 90, 45, 67 }; for (int i = 0; i <= 3; i++) { cout << marks[i] << endl; } What is the result of executing this code snippet?
The code snippet has a bounds error or displays redundant data.
What is the error in the following code snippet, which is used for calculating the average score for a student in three subjects? #include <iostream> int main() { int subject1 = 75; int subject2 = 65; int subject3 = 70; int average = subject1 + subject2 + subject3 / 3; cout << "The average is " << average; return 0; }
The code snippet has a logic error.
What is the output of the following code snippet? int counter = 0; counter++; cout << "The initial value of the counter is " << count << endl;
The code will not compile
When a compiler finds a syntax error in a program, what happens?
The compiler continues and may report about other errors but does not produce an executable file.
What is result of evaluating the following expression? (45 / 6) % 5
The correct answer is: 2
Assuming that a user enters 25 as the value for x, what is the output of the following code snippet? int x = 0; cout << "Enter number: "; cin >> x; if (x < 100) { x = x + 5; } if (x < 500) { x = x - 2; } if (x > 10) { x++; } else { x--; } cout << x << endl;
The correct answer is: 29
Consider a situation where you are planning on purchasing a new cable TV dish. You are considering two cable TV dishes. These cable TV dishes have different purchase prices. Each channel service provider charges a different rate for each month that the cable TV dish is used. To determine which cable TV dish is a better buy, you need to develop an algorithm to calculate the total cost of purchasing and using each cable TV dish. What are all of the inputs that you need for this algorithm?
The cost of each cable TV dish, the rate per month for using each cable TV dish, and the number of months you would use the cable TV dish
Consider a situation where you are planning on purchasing a new cell phone. You are considering two cell phones. These cell phones have different purchase prices. Each mobile service provider charges a different rate for each minute that the cell phone is used. To determine which cell phone is the better buy, you need to develop an algorithm to calculate the total cost of purchasing and using each cell phone. What are all the inputs needed for this algorithm?
The cost of each cell phone, the rate per minute for each cell phone, and the number of minutes you would use the cell phone
Which loop does not check a condition at the beginning of the loop?
The do-while loop
Consider the following code snippet: vector<int> num(3); num[0] = 1; num[1] = 2; num[2] = 3; num.pop_back(); Which element or elements of the vector are removed by the pop_back() function in the given code snippet?
The element with index 2
Consider the following code snippet: int numarray[6]; for (int cnt = 1; cnt < 6; cnt++) { numarray[cnt] = cnt + 1; } Which one of the following statements is correct about the given code snippet?
The for loop initializes all the elements except the first element.
If a function is declared to return "void", then which statement below is true?
The function needs a return statement with no return value
What is the problem with the code snippet below? string val() { string result = "candy"; return; } int main(){ cout << val() << endl; return 0; }
The function val does not have a return value.
Say you are writing an algorithm to arrange tiles to fill a rectangular floor with alternating black and white tiles. What would be the required inputs to the algorithm?
The length and width of the floor and of the tiles
An example of an input device that interfaces between computers and humans is
The microphone
Assuming that the user inputs a value of 25 for the price and 10 for the discount rate in the following code snippet, what is the output? int main() { cout << "Enter the price: "; double price; cin >> price; cout << "Enter the discount rate: "; double discount; cin >> discount; cout << "The new price is " << price - price * (discount / 100.0) << endl; return 0; }
The new price is 22.5
Assuming that a user enters 15 as input, what is the output of the following code snippet? int main() { int number; cout << "Please enter a number: "; cin >> number; if (number > 20) { cout << "The number is LARGE!" << endl; } else { cout << "The number is SMALL!" << endl; } return 0; }
The number is SMALL!
An example of an output device that interfaces between computers and humans is
The speaker
What is the result of the following definition of a vector? vector<int> chkdata;
The statement creates a vector of size 0.
What is the syntax error in the following function definition? string parameter(double r) { double result; result = 2 * 3.14 * r; return result; }
The value that is returned does not match the specified return type
What is the syntax error in the following function definition? string area(double r) { double a; a = 3.14 * r * r; return r * r; }
The value that is returned does not match the specified return type.
What should be ensured for calculating the largest value in a vector?
The vector contains at least one element.
What should be ensured for calculating the smallest value in a vector?
The vector contains at least one element.
What, if any, is the syntax error in the following statement? double result = (-(b * b - 4 * a * c) / pow(a,2);
There are unbalanced parentheses
What is the output of the following code snippet? #include <iostream> #include <string> using namespace std; int main() { int var1 = 10; int var2 = 2; int var3 = 20; var3 = var3 / (var1 % var2); cout << var3 << endl; return 0; }
There will be no output due to a run-time error.
Why are the @ symbols used in these function comments? /** Computes the area of a rectangle. @param width the width of the rectangle @return the area of the rectangle */ double area(double width, double height) { double area = width * height; return area; }
They are special symbols for the Doxygen documentation-generator utility tool.
What is the reason for including the following code snippet in the header file Animal.h? #ifndef ANIMAL_H #define ANIMAL_H class Animal { public: Animal(); Animal(double new_area_hunt); void birth(); void hunt(double new_area_hunt); void death(); double get_area_hunt() const; private: double area_hunt; }; #endif
To define the interface for the Animal class
What is the purpose of the following algorithm? num = 0 Repeat the following steps 10 times input var1 if var1 > num then num = var1 end of if end of repeat print num
To find the highest among 10 numbers
What is the purpose of the following algorithm? somenum = 0 Repeat the following steps 50 times input variable1 if variable1 > somenum then somenum = variable1 end of if end of repeat print somenum
To find the highest among 50 numbers
The purpose of a function that returns "void" is
To package a repeated task as a function even though the task does not yield a value
Consider a situation where you are buying books online. The bookseller charges $19.95 as the price per book and $4.95 as the handling cost for up to three books. For every book purchased in addition to three books, there is a handling charge of $1.50. In addition, there is a 7% tax on the cost of the books but not on the handling charges. Assuming that num_books represents the number of books you are purchasing, which of the following is the correct algorithm for calculating the total cost of the books that you are purchasing?
Total charge for the books = 4.95 * num_books Tax on the books = Total charge for the books * .07 if (books <= 3) then Handling charges = 4.95 else Handling charges = 4.95 + 1.50 * (num_books - 3) Total cost of order = total charge for books + tax + handling charges
Consider a situation where you are buying videos online. The video seller charges $21.50 as the price per video and $6.75 as the handling cost for up to three videos. For every video purchased in addition to three videos, there is a handling charge of $1.50. In addition, there is a 9% tax on the cost of the videos but not on the handling charges. Assuming that num_videos represents the number of videos you are purchasing, which of the following is the correct algorithm for calculating the total cost of the videos that you are purchasing?
Total charge for the videos = 21.50 * num_videos Tax on the videos = total charge for videos * .09 if (num_videos <= 3) then Handling charges = 6.75 else Handling charges = 6.75 + 1.50 * (num_videos - 3) Total cost of order = total charge for videos + tax + handling charges
What is the output of this code snippet? double average; int grade1 = 87; int grade2 = 94; // cout << "The average is " << (grade + grade2) / 2.0 << endl; cout << "The average is " << average << endl;
Unpredictable result
What is the output of the following code snippet? int main() { string str1; str1 = "I LOVE MY COUNTRY"; string str2 = str1.substr(4, 5); cout << str2 << endl; return 0; }
VE MY
In what situations is a do...while() loop preferred over a while() loop?
When asking for an unknown number of user input values.
In what situations is a while() loop preferred over a do...while() loop?
When the correct number of iterations for the loop may be zero.
In what situations is a for() loop more appropriate than a while() loop?
When the number of iterations is known ahead of time
When should comments be written to describe functions?
Whenever you write a function
For a program that reads city names repeatedly from the user and calculates the distance from Head Office, which of the following would be a good design based on stepwise refinement?
Write one function that reads city name and calculates distance.
bool i = false; do { cout << "What do you C?" << endl; } while (i != i);
Yes, it is legal and prints "What do you C?" once.
Assuming that a user enters 25 as input, what is the output of the following code snippet? int main() { int age; cout << "Please enter a number: "; cin >> age; if (age > 30) { cout << "You are wise!" << endl; } else { cout << "You have much to learn!" << endl; } return 0; }
You have much to learn!
Which one of the following is an assignment statement?
a = 20;
An object of a class may have which of the following items?
a. Mutator member functions b. Data members c. Accessor member functions d. All of the listed items
Which of the following statements represents a logic error, but not a compile-time error?
cout << "The sum of 5 and 6 is 10" << endl;
Which of the following statements would generate a compile-time error?
cout << eleven << endl;
In an airline reservation system, the cost of an airline ticket is required. Which data type should be used to store this value?
double
Which of the following options defines a double variable?
double age;
Suppose you need to write a function that calculates the area of a rectangle. Which of the following is the best choice for the declaration of this function?
double area(double w, double h)
Consider a function named avg, which accepts four numbers as integers and returns their average as a double. Which of the following is the correct statement to call the function avg?
double average = avg(2, 3, 4, 5);
Which of the following is the correct first line of a function definition named calc_average that accepts three int parameters and returns a double?
double calc_average(int a, int b, int c)
Which of the following is the correct first line of a function definition named calc_sum that accepts four int parameters and returns a double?
double calc_sum(int a, int b, int c, int d)
A store applies a 15% service charge on all items with a price of at least $150. No service charge is otherwise applicable. Which of the following statements DOES NOT correctly compute the service charge?
double service_charge = 0.15 * cost; if (cost <= 150) { service_charge = 0; }
In the following class definition, which of the data members or member functions are hidden from the class users? class Car { public: Car(); void start(); void stop(); private: double speed; void set_speed(double new_speed); }; Select one:
double speed and void set_speed(double new_speed)
What is the difference between the float and double types in C++?
double usually takes twice as many bytes of memory storage as float.
Suppose you need to write a function that calculates the volume of rectangular boxes. Which of the following is the best choice for the declaration of this function?
double volume(double w, double h, double l)
Computers are machines that
execute programs
What is the output of the following code snippet? #include <iostream> #include <string> using namespace std; int main() { string some_string1 = "his"; string some_string2 = "cycle"; if (some_string1 < some_string2) { cout << some_string2; } else { cout << some_string1; } return 0; }
his
Suppose one needs an if statement to check whether an integer variable pitch is equal to 440 (which is the frequency of the note "A" to which strings and orchestras tune). Which condition is correct?
if (pitch == 440)
Characters that are grouped together between double quotes (quotation marks) in C++ are called
strings
The switch statement in C++
is like a sequence of if statements that compares a single integer value against several constant alternatives
The instruction "1011 0011" for a computer is an example of
machine language.
Consider the following C++ statement: string name = "Harry"; Which of the following is true?
name is a string variable.
How do you compute the length of the string str?
str.length()
How do you extract first 5 characters from the string str?
str.substr(0, 5)
Which of the following is the best choice for a return type from a function that prompts users to enter their credit card number?
string
Which of the following statements is correct about an accessor member function?
It returns the value of a data member of an object but does not modify any data member values.
What is the meaning of num = 10; in C++?
It sets the variable num to ten.
What is the meaning of x = 0; in C++?
It sets the variable x to zero.
What is the output of the following code snippet? int main() { int num = 100; if (num < 100) { if (num < 50) { num = num - 5; } else { num = num - 10; } } else { if (num > 150) { num = num + 5; } else { num = num + 10; } } cout << num << endl; return 0; }
110
What output is produced by these statements? string city = "Rio de Janeiro"; cout << city.length();
14
What is the difference between an editor and a compiler?
An editor allows program files to be written and stored; a compiler converts program files into an executable program
An integrated development environment (IDE) bundles tools for programming into a unified application. What kinds of tools are usually included?
An editor and a compiler
What is a logic error?
An error that occurs when a program is running because, for example, the wrong operator was used.
In the given code snippet, what type of member function is get_data()? class Building { public: void get_data() const; private: double height; double no_stories; }; void Building::get_data() const { cout << height << endl; cout << no_stories << endl; }
Accessor member function
In the given code snippet, what type of member function is view()? class CashRegister { public: void view() const; private: int item_count; double total_price; }; void CashRegister::view() const { cout << item_count << endl; cout << total_price << endl; }
Accessor member function
In the following code snippet, which member function of the class AeroPlane is called first? class AeroPlane { public: void flyup(); void accelerate(double new_velocity); void flydown(); double get_velocity() const; AeroPlane(); private: double velocity; }; AeroPlane::AeroPlane() { velocity = 0; } void AeroPlane::flyup() { accelerate(get_velocity() + 10); } void AeroPlane::flydown() { velocity = 0; } void AeroPlane::accelerate(double new_velocity) { velocity = velocity + new_velocity; } double AeroPlane::get_velocity() const { return velocity; } int main() { AeroPlane c1; c1.flyup(); c1.accelerate(10); c1.get_velocity(); c1.flydown(); return 0; }
AeroPlane()
Consider this function comment. Which of the following options is recommended in your textbook? /** Computes the area of a rectangle. @param width the width of the rectangle @return the area of the rectangle */ double area(double width, double height) { double area = width * height; return area; }
Both parameters should be described.
How are variables passed as input to a function?
By using arguments
The ceil function in the C++ standard library takes a single value x and returns the smallest integer that is greater than or equal to x. Which of the following is true about ceil(12.6)?
The argument is 12.6, and the return value is 13.
Assuming that a user enters 64 as his score, what is the output of the following code snippet? int main() { int score = 0; cout << "Enter your score: "; cin >> score; if (score < 40) { cout << "F" << endl; } else if (score >= 40 || score < 50) { cout << "D" << endl; } else if (score >= 50 || score < 60) { cout << "C" << endl; } else if (score >= 60 || score < 70) { cout << "B" << endl; } else if (score >= 70 || score < 80) { cout << "B+" << endl; } else { cout << "A" << endl; } return 0; }
D
Which of the following is correct about a global variable?
It is visible to all the functions declared after it.
What is true about a mutator member function?
It modifies one or more data members of an object.
Consider a situation where you are assigned to develop an algorithm to calculate the total cost of a purchase order that contains several items. The cost of each item and the tax rate is known. The standard shipping charge for the entire order is $4.95, and the special delivery charge is $19.95. In addition, there is no tax on the shipping cost. Which of the following is the correct pseudocode for the required algorithm?
For each item on the purchase order: Order cost = order cost + item cost If standard shipping Shipping cost = 4.95 Else Shipping cost = 19.95 Total purchase order cost = order cost * tax rate + shipping cost
Which of the following is true about functions?
Function can have multiple parameters and can return one return value.
Computer scientists have devised something that allows programmers to describe tasks in words that are closer to the syntax of the problems being solved. This is called
High level programming language
If you were creating an application for a hospital, which entities would be appropriately defined as classes? I. Doctors II. Patients III. Nurses
I, II, III
What statements about the integrated development environment (IDE) are true? I. You may run an executable program even after exiting the IDE II. The IDE contains a program called the linker, which is required to build an executable program III. Translating C++ source code into machine code is not enough to actually run the program
I, II, III
Given int apples = 5; Which line(s) of code are the equivalent of: apples += 1; I. apples = apples; II. apples = apples + 1; III. apples++;
II and III
Consider the given scenario for describing an algorithm using pseudocode. UML Supermarket has different ways of awarding discounts to its customers for each purchase they make. A 10% discount is given on the total value of the purchase. In addition, a standard loyalty discount is given if customers have a permanent customer card. Your program should indicate the amount payable by the customer after the discounts. Identify the inputs that the program requires from the given set of options. I. The discount percentage II. The total value of the purchase III. The loyalty-discount amount IV. The customer card number V. The amount payable after discount
II and IV
Consider the given scenario for describing an algorithm using pseudocode. WALMART Supermarket has different ways of awarding discounts to its customer IDs for each purchase they make. An 8% discount is given on the total value of the purchase. In addition, a standard loyalty discount is given if customers have a permanent customer ID card. Your program should indicate the amount payable by the customer after the discounts. Identify the inputs that the program requires from the given set of options. I. The discount percentage II. The total value of the purchase III. The loyalty-discount amount IV. The customer ID card number V. The amount payable after discount
II and IV
Which one of the following typically provides data persistence without electricity? I. The CPU's memory II. The hard disk III. Secondary storage
II, III
Consider the following statements about computer programs: I. Computer programs can be written by someone who has a basic knowledge of operating a computer. II. Computer programs can complete complex tasks quickly. III. Large and complex computer programs are generally written by a group of programmers. IV. Computer programs are composed of extremely primitive operations. Which one of the following options is correct?
II, III, and IV are correct statements.
Consider the following code snippet: vector<int> series1(10); vector<int> series2(10); for (int i = 0; i < series1.size(); i++) { series1[i] = i + 1; series2[i] = series1[i] + 1; } What values are stored in the 0th element of the series1 and series2 vectors?
In the code snippet, series1 has 1 and series2 has 2.
In the following code snippet, what is the scope of variable b? void func1() { int i = 0; double b = 0; } void func2() { } int main() { func1(); func2(); return 0; }
It can be used only in func1().
Which of the following is a benefit of encapsulation?
It guarantees that an object cannot be accidentally put into an inconsistent state.
Consider the member function call reg.add_item(2, 19.95); Which of the following describes the role of reg in this call?
It is an implicit parameter.
A function uses a reference parameter when
It is designed to update a variable supplied as an argument
What is wrong with this algorithm for sorting a deck of playing cards according to suit? Pick up the top card from the deck Put it into the pile corresponding to its suit (heart, diamond, spade, club) Repeat
It is not terminating.
What is one of the benefits of using a high-level programming language like C++?
Problems solved in a high-level language are independent of the underlying computer hardware
Assuming that a user enters 64 as his golf score, what is the output of the following code snippet? int main() { int golf_score = 0; cout << "Enter your golf score: "; cin >> golf_score; if (golf_score < 60) { cout << "Astounding!" << endl; } if (golf_score >= 60 && golf_score < 70) { cout << "Professional!" << endl; } if (golf_score >= 70 && golf_score < 80) { cout << "Pretty good!" << endl; } if (golf_score >= 80 && golf_score < 90) { cout << "Not so hot!" << endl; } if (golf_score >= 90) { cout << "Keep your day job!" << endl; } return 0; }
Professional!
Consider a scenario in which you develop a C++ program on a computer that has a Pentium processor and compile the program into the corresponding machine language. What step should you take to run the same program on a computer that has a different processor?
Recompile the C++ program on the computer that has a different processor.
The programmer, not the compiler, is responsible for testing a program to identify
Run-time errors
Which one of the following is defined as a sequence of characters?
String
What kind of error is created by the following code snippet? coutt << "Hello, World!" << endl;
Syntax error: the program will not compile
Which one of the following errors represents a part of a program that is incorrect according to the rules of the programming language?
Syntax errors
How do programmers find exceptions and run-time errors?
Testing the compiled program with a variety of input values
Consider this function comment. Which of the following options is recommended in your textbook? /** Computes the area of a cuboid. @param1 width the width of the cuboid @param2 height the height of the cuboid @param3 length the length of the cuboid @return the area of the cuboid */ double area(double width, double height, double length) { double result = width * height * length; return result; }
The @param1, @param2, @param3 should be just @param.
Computer programming is
The act of designing and implementing a computer program
What is the problem with the following algorithm? Repeat a number of times Add sales amount to total sales.
The algorithm is ambiguous because it does not specify how many times to repeat the Add statement.
Given the definition const double PI = 3.14159; which of the following is the C++ equivalent of the mathematical expression c = p·radius2
c = PI * pow(radius, 2)
A network controller is the device in a computer that
connects the computer to the internet or other network through a wire or wireless.
A recursive function is one which:
calls itself.
Programs that are not running are usually stored
in secondary storage.
In an airline reservation system, the number of available seats in an airplane is required. Which data type should be used to store this value?
int
Which of the following options defines an integer variable?
int age;
What changes do you need to make in the following code snippet to display "Can you C?" exactly 5 times? int i = 0; while (i <= 5) { cout <<"Can you C?"<< endl; i++; }
int i = 1;
What changes do you need to make in the following code snippet to display "Let us C" exactly 10 times? int i = 0; while (i <= 10) { cout << "Let us C" << endl; i++; }
int i = 1;
Identify the correct statement for defining an integer array named numarray of ten elements.
int numarray[10];
Identify the correct statement for defining an integer array named numcount of five elements.
int numcount[5];
Consider a function named calc, which accepts two numbers as integers and returns their sum as an integer. Which of the following is the correct statement to call the function calc?
int sum = calc(2, 3);
Consider the following code snippet. Assuming that the user enters 20 and 12 as the two input values, what is the output of the code snippet? int main() { int num1 = 0; int num2 = 0; int num3 = 0; int num4 = 0; int num5 = 0; cout << "Enter a number: "; cin >> num1; cout << "Enter a number: "; cin >> num2; if (num1 < num2) { num3 = num1; } else { num3 = num2; } if (num1 < num2 + 10) { num4 = num1; } else if (num1 < num2 + 20) { num5 = num1; } cout << "num1 = " << num1 << " num2 = " << num2 << " num3 = " << num3 << " num4 = " << num4 << " num5 = " << num5 << endl; return 0; }
num1 = 20 num2 = 12 num3 = 12 num4 = 20 num5 = 0
What are the values of num1 and num2 after this snippet executes? double num1 = 4.20; double num2 = num1 * 10 + 5.0;
num1 = 4.20 and num2 = 47.0
Given the definition const double PI=3.14159; which of the following is the C++ equivalent of the mathematical expression p = 2· p·(radius)3
p = 2 * PI * pow(radius, 3);
Assuming that a user enters 22 as the price of an object, which of the following hand-trace tables is valid for the given code snippet? int price = 0; string status = ""; cout << "Please enter object's price: "; cin >> price; if (price >= 50) { status = "reasonable"; if (price >= 75) { status = "costly"; } } else { status = "inexpensive"; if (price <= 25) { status = "reasonable"; } }
price status 0 "" 22 "inexpensive" "reasonable"
An algorithm should be written first in:
pseudocode.
What is the result of the following snippet? int son_age = 5; int father_age = son_age * 3 + 5;
son_age = 5 and father_age = 20
Which of the following code snippets is legal for implementing the CashRegister class member function named add_item?
void CashRegister::add_item(double price) { item_count = item_count + 1; total_price = total_price + price; }
Which of the following code snippets is legal for implementing the Shopping_Cart class member function named add_product? Assume that the Shopping_Cart class contains data members product_count and total_price.
void Shopping_Cart::add_product(double price) { product_count = product_count + 1; total_price = total_price + price; }
Which is the appropriate time to initialize a variable?
when you define it