You are on page 1of 36


Home Flashcards Chemical Engineering Quizzes

Shared Flashcard Set

Psychologist No Fault
Injury
Behavioral Medicine Assoc

New York and Pennsylvania Psychologists


Treat Injured workers with chronic pain &
anxiety

OPEN

Details

Title
Quizzes

Description
In class quizzes

Total Cards
75

Subject
Chemical Engineering

Level
Undergraduate 2

Created
03/18/2012

Click here to study/print these flashcards.

Create your own flash cards! Sign up here.

Additional Chemical Engineering Flashcards

 
 

Cards

Term

How are variables passed as input to a function?

Definition

By using arguments

Term

What is the error in the following function definition?

int tripler(int num_para)


{
double result = num_para * 3;
}

Definition

The function does not return a value.

Term
What is the output of the following code snippet? #include using namespace std; 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; }
Definition

16

Term

The purpose of a function that returns “void” is

Definition

To package a repeated task as a function even though the task does not yield a value

Term

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)?

Definition

The argument is 12.6, and the return value is 13.


Term

What is the output of the following code snippet? #include using namespace std; void prevnext(int a, int&
prv, int nxt) { prv = a - 1; nxt = a + 1; } int main() { int a = 100; int b = 0; int c = 0; prevnext(a, b, c); cout <<
"Previous = " << b << ", Next = " << c; return 0; }

Definition

Previous = 99, Next = 0

Term

What is the output of the following code snippet? #include using namespace std; void double_amount(int
in_value) { in_value = 2 * in_value; } int main() { int principal_amt = 2000; double_amount(principal_amt);
cout << principal_amt << endl; return 0; }

Definition

2000

Term

A function uses a reference parameter when


Definition
It is designed to update a variable supplied as an argument

Term

#include using namespace std; void func1() { int i = 0; double b = 0; } void func2() { // empty } int main() {
func1(); func2(); return 0; }

Definition

It can be used only in func1().

Term

It can be used only in func1().

Definition

The function does not specify a type for the second parameter variable.

Term

What are the values of x and y after executing the given code snippet? #include using namespace std;
void mystery(int& a, int& b) { a = b; b = a; } int main() { int x = 10; int y = 11; mystery(x, y); return 0; }
Definition

x = 11 and y = 11

Term

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?

Definition

A return statement.

Term

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?

Definition

double volume(double w, double h, double l)

Term
#include using namespace std; int my_fun(int perfect) { { int perfect = 0; return ((perfect – 1) * (perfect –
1)); } return perfect; } int main() { for (int i = 0; i < 4; i++) { cout << my_fun(i) << " "; } return 0; }

Definition

1111

Term

The statements that are executed when a function is called are known as:

Definition

The body of the function

Term

What is incorrect in the following code snippet? #include using namespace std; void display_box(string
str) { cout << "--------" << endl; cout << str << endl; cout << "--------" << endl; } int main() { cout <<
display_box("Hello World"); return 0; }

Definition

display_box does not return a value; therefore, it cannot be used with cout <<.
Term

How many times will the cout statement execute in the following code snippet?

for (int num2 = 1; num2 <= 3; num2++)


{
for (int num1 = 0; num1 <= 2; num1++)
{
cout << num2 << " " << num1 << endl;
}
}

Definition

9 times

Term

What will be the output of the following code snippet?

for (int i = 0; i < 6; i++)


{
for (int j = 7; j > i; j--)
{
cout << "+";
}
cout << endl;
}

Definition

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.

Term

Which common error is present in the code below, which is intended to calculate the average value from a
sum of numbers?
double total = 0.0;
int n = 0;
double input;
while (cin >> input)
{
total = total + input;
n++;
}
double average = total / n;

Definition

Division by zero

Term

The code snippet below checks whether a given number is a prime number.

int j = 2;
int result = 0;
int number = 0;
cout << "Please enter a number: ";
cin >> number;
while (j <= number / 2)
{
if (number % j == 0)
{
result = 1;
}
j++;
}
if (result == 1)
{
cout << "Number: " << number << " is Not Prime! ";
}
else
{
cout << "Number: " << number << " is Prime! ";
}

What will be the result of executing this code snippet?


Definition

The code snippet will display the desired result without errors
Term

What will be the final output of the following code snippet when a user enters input values in the order 10,
20, 30, 40, 50, and -1?

double sum = 0;
int count = 0;
double salary = 0;
double average = 0;
cout << "Enter salaries (-1 to stop): " << endl;
while (salary != -1)
{
cin >> salary;
if (salary != -1)
{
sum = sum + salary;
count++;
}
}
if (count > 0)
{
average = sum / count;
cout << "The Average Salary: " << average << endl;
}
else
{
cout << "No data!" << endl;
}

Definition

The Average Salary: 30

Term

What is the output of the code snippet given below?

string s = "12345";
int i = 1;
do
{
if (i > 1)
{
cout << s.substr (i, 1);
}
}
while (i < 5);
Definition

No output (Infinite loop)

Term

What is the output of the code fragment given below?

int i = 0, j = 0;
while (i < 125)
{
i = i + 2;
j++;
}
cout << j << endl;

Definition

63

Term

Is the following code snippet legal?

bool i = false;
do
{
cout << "What do you C?" << endl;
}
while (i != i);

Definition

Yes, it is legal and prints "What do you C?" once.


Term

What is the output of the code snippet given below?

string s = "abcde";
int i = 1;
while (i < 5)
{
cout << s.substr (i, 1);
i++;
}

Definition

bcde

Term

What will be the output of the following code snippet?

int i;
int j;
for (i = 0; i < 7; i++)
{
for (j = 7; j > i; j--)
{
cout << "+";
}
cout << endl;
}

Definition

An inverted right triangle with seven rows and seven columns of the plus sign. The number of columns
decrements by one on completion of one iteration of the inner loop.

Term

What is the output of the code snippet given below?


string s = "12345";
int i = 1;
while (i < 5)
{
cout << s.substr (i, 1);
i++;
}

Definition

2345

Term

The Monte Carlo method can find solutions to problems that are difficult to solve precisely. A typical
example of such a problem is

Definition

The approximation of the value of pi

Term

What will be the result of running the following code fragment?

int time = 0;
int year = 0;
int rate = 0;
int principal = 1000;
int interest = 0;
while (year < 10)
{
interest = (principal * time * rate) / 100;
cout << interest << endl;
}

Definition

The code fragment will continue to display the calculated interest because the loop will never end.

Term

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;
}
}

Definition

139

Term

How many times does the loop run in the following code fragment?

for (int i = 0; i < 60; i = i + 4)


{
cout << i << endl;
}

Definition
15

Term

Assuming that a user enters 64 as his score, what is the output of the following code snippet?

cout << "Enter your score: ";


int 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;
}

Definition

Term

Assuming that a user enters 68 as his score, what is the output of the following code snippet?

int score = 0;
cout << "Enter your score: ";
cin >> score;
if (score < 50)
{
cout << "F" << endl;
}
else if (score >= 50 || score < 55)
{
cout << "D" << endl;
}
else if (score >= 55 || score < 65)
{
cout << "C" << endl;
}
else if (score >= 65 || score < 75)
{
cout << "B" << endl;
}
else if (score >= 75 || score < 80)
{
cout << "B+" << endl;
}
else
{
cout << "A" << endl;
}

Definition

Term

What is the output of the following code snippet if the input is 25?

cout << "Please enter a number: ";


int i;
cin >> i;

if (i > 25)
{
i++;
}
else
{
i--;
}
cout << i << endl;
Definition

24

Term

What is the output of the following code snippet?

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;

Definition

110

Term

Assuming that the user inputs "twenty" as the input, what is the output of the following code snippet?

cout << "Please enter your age: ";


int age;
cin >> age;
if (cin.fail())
{
cout << "Invalid Data!" << endl;
return 0;
}
if (age < 18)
{
cout << "You are not eligible to vote!";
return 0;
}
else
{
cout << "You are a valid voter!" << endl;
if (age > 60)
{
cout << "You are a senior voter!" << endl;
}
else
{
cout << "You are not a senior voter!" << endl;
}
}

Definition

Invalid Data!

Term

Consider the following code snippet. Assuming that the user inputs 75 as the age, what is the output?

cout << "Please enter your age: ";


int age;
cin >> age;

if (age < 10)


{
cout << "Child ";
}
if (age < 30)
{
cout << "Young adult ";
}
if (age < 70)
{
cout << "Old ";
}
if (age < 100)
{
cout << "Impressively old ";
}

Definition

Impressively old

Term

Consider the following code snippet:

cout << "Enter number: ";


int number;
cin >> number;

if (number > 30)


{
..........
}
else if (number > 20)
{
..........
}
else if (number > 10)
{
..........
}
else
{
..........
}

Assuming that the user input is 40, which of the following block of statements is executed?

Definition

if (number > 30) { .......... }

Term

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 num1, num2, num3 = 0, num4 = 0, num5 = 0;

cout << "Enter two numbers: ";


cin >> num1 >> 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;
Definition

num1 = 20 num2 = 12 num3 = 12 num4 = 20 num5 = 0

Term

In a switch statement, if a break statement is missing

Definition

Execution falls through the next branch until a break statement is reached
Term

Assuming that a user enters 25 as input, what is the output of the following code snippet?

cout << "Please enter a number: ";


int age;
cin >> age;

if (age > 30)


{
cout << "You are wise!" << endl;
}
else
{
cout << "You have much to learn!" << endl;
}

Definition

You have much to learn!

Term

What is the conditional required to check to see if the length of a string s1 is odd?

Definition

if ((s1.length() % 2) != 0)
Term

Assuming that a user enters 25 as the value for x, what is the output of the following code snippet?

cout << "Enter number: ";


int x;
cin >> x;

if (x < 100)
{
x = x + 5;
}
if (x < 500)
{
x = x - 2;
}
if (x > 10)
{
x++;
}
else
{
x--;
}
cout << x << endl;

Definition

29

Term

What is the output of the following code snippet?

double salary = 55000;


double cut_off = 65000;
double min_salary = 40000;

if (min_salary > salary)


{
cout << "Minimum salary requirement is not met." << endl;
}
if (cut_off < salary)
{
cout << "Maximum salary limit is exceeded." << endl;
}
else
{
cout << "Salary requirement is met." << endl;
}
Definition

Salary requirement is met.

Term

Assuming that the user provides 303 as input, what is the output of the following code snippet?

int x = 0;
cout << "Please enter y: ";
int y;
cin >> y;

if (y > 300)
{
x = y;
}
else
{
x = 0;
}
cout << "x: " << x << endl;
Definition

x: 303

Term

Assuming that a user enters 50, 70, and 60 as input values one after another, separated by spaces, what is
the output of the following code snippet?

int number1;
int number2;
int number3 = 0;

cout << "Enter a digit ";


cin >> number1;

cout << "Enter a digit: ";


cin >> number2;

cout << "Enter a digit: ";


cin >> number3;

if (number1 > number2)


{
if (number1 > number3)
{
cout << number1 << endl;
}
else
{
cout << number3 << endl;
}
}
else
{
if (number2 > number3)
{
cout << number2 << endl;
}
else
{
cout << number3 << endl;
}
}
Definition

70

Term

Assuming that the user provides 101 as input, what is the output of the following code snippet?

cout << "Please enter b: ";


int a, b;
cin >> b;

if (b > 100)
{
a = b;
}
else
{
a = 0;
}
cout << "a: " << a << endl;
Definition

a: 101

Term

Assuming that a user enters 56 as his age, what is the output of the following code snippet?

cout << "Enter your age: ";


int age;
cin >> age;

if (age < 13)


{
cout << "Kid!" << endl;
}
if (age >= 13 && age < 19)
{
cout << "Teen!" << endl;
}
if (age >= 19 && age < 30)
{
cout << "Young!" << endl;
}
if (age >= 30 && age < 50)
{
cout << "Adult!" << endl;
}
if (age >= 50)
{
cout << "Old!" << endl;
}
Definition
Old!

Term

Assuming that a user enters 64 as his golf score, what is the output of the following code snippet?

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;
}
Definition

Professional!

Term

What are the values of num1 and num2 after this snippet executes?

double num1 = 4.20;


double num2 = num1 * 10 + 5.0;
Definition

num1 = 4.20 and num2 = 47.0

Term
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?

Definition

int

Term

What is the value inside the var variable at the end of the given code snippet? #include using namespace
std; int main() { int var = 30; var = var + 2 / var; var++; return 0; }

Definition

31

Term

What is the output of the following code snippet if the user provides NEW YORK as the input? #include
#include using namespace std; int main() { string name; cout << "ENTER THE STRING: "; cin >> name; int
s1 = name.length(); cout << s1 << endl; return 0; }

Definition

3
Term

What is the output of the following code snippet? #include #include using namespace std; int main() { int
var1 = 10; int var2 = 2; int var3 = 20; var3 = var3 / (var1 % var2); cout << var3 << endl; return 0; }

Definition

There will be no output due to a run-time error.

Term

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 #include 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; }

Definition

4562

Term

What is the correct way to invoke functions on variables in C++ that are strings?
Definition

Invoke them using the variable name and the dot (.) notation
Term

Which of the following choices is a valid statement about this code snippet? #include using namespace
std; int main() { double d = 45.326; double r = d % 9.0; cout << r; return 0; }

Definition

The assignment statement for variable r is wrong, because the % operator expects integer values as
operands

Term

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? #include using namespace std; 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; }

Definition

The new price is 22.5

Term

Which one of the following operators computes the remainder of an integer division?
Definition

%
Term

Consider the following code snippet:

cout << "Please enter a number: ";


int num1;
cin >> num1;
cout << "Please enter another number: ";
int num2;
cin >> num2;
cout << "The product is " << num1 * num2 << endl;

Suppose the student provides the answer 2 5 [Enter] as input to the first prompt. What will the program
do?
Definition

Print "The product is 10"

Term

Which is the appropriate time to initialize a variable?

Definition

When you define it

Term
What is the result of the following snippet?

int son_age = 5;
int father_age = son_age * 3 + 5;

Definition

son_age = 5 and father_age = 20

Term

What is the output of the following code snippet? #include using namespace std; int main() { double a; a =
sqrt(9.0) + sqrt(16.0); cout << a << endl; return 0; }

Definition

7.0

Term

What is wrong with the following code snippet? #include using namespace std; int main() { int width = 10;
height = 20.00; cout << "width = " << width << " height = " << height << endl; return 0; }

Definition
The code snippet uses an undefined variable.

Term

How many integer elements can be stored in the array myarr declared below?
--
int myarr[10][8];

Definition

80

Term

Consider the following code snippet:


--
int arr[3][3] =
{
{ 1, 2, 3 },
{ 4, 5, 6 }
};
int val = arr[0][2] + arr[2][0];
cout << val;
--
What is the output of the given code snippet on execution?
Definition

Term
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?
Definition

3040

Term

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?
Definition

Term

Consider the following code snippet:


--
int numarray[2][3] = { { 3, 2, 3 }};
cout << numarray[0][0];
cout << numarray[1][0];
--
What is the output of the given code snippet?
Definition

30

Term

Consider the following code snippet:


--
for (int cnt = 1; cnt < 3; cnt++)
{
num[cnt] = cnt + 1;
cout << num[cnt];
}
--
What is the output of the given code snippet?

Definition

23

Term

Consider the following code snippet:


--
int numarray[] = { 1, 2, 3, 4, 5 };
cout << numarray[1];
cout << numarray[4];
--
What is the output of the code snippet?

Definition

25

Term
What is the valid range of index values for an array of size 10?
Definition

0 to 9

Term

Consider the following code snippet:


--
int numarray[2][3] = {{8, 7, 6}};
cout << numarray[0][0];
cout << numarray[1][0];
--
What is the output of the given code snippet?

Definition

80

Term

What should be ensured for calculating the smallest value in a vector?

Definition

The vector contains at least one element.


Term

Consider the following code snippet:


--
int arr[2][3] =
{
{ 13, 23, 33 },
{ 14, 24, 34 }
};
--
What is the appropriate statement to display the value 24 from the given array?

Definition

cout << arr[1][1];

MY FLASHCARDS

FLASHCARD LIBRARY

BROWSE

ABOUT

HELP

MOBILE

Create Account

© 2001 - 2020 Flash Card Machine, LLC

You might also like