wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Quiz 2: C++ Basics

Total questions: 11

Worksheet time: 8mins

Name
Class
Date
1.

Given the following string variable declaration and statements:

string title;

int i;

title = "One String";

which of the following is the correct statement that assigns the length of the variable title to the variable i?

a)

i = One String.length();

b)

i = string.length();

c)

i = title.length();

d)

i = length();

2.

Given the following string variable declaration and statements:


string title;

int j;

title = "This is Adam";

cout << title.substr(1,5) << endl;


What is the correct output?

(a)  

3.

Write a C++ statement that declares the variable firstInitial to be a character variable.

(a)  

4.

Given the following string variable declaration:

string name;

string lastName = "Green";

string firstName = "Cathy";

which of the following statement is the correct statement that assigns Green, Cathy to the variable name.

a)

name = "Green Cathy";

b)

name = lastName +", " + firstName;

c)

name = Green, Cathy;

d)

name = Green + ", " + Cathy;

5.

What is the result of the following expression?

9/8

(a)  

6.

What is the result of the following expression?

11%9

(a)  

7.

Which of the following is the correct function call to calculate 12^5

a)

power(12,5)

b)

power(5,12)

c)

pow(12,5)

d)

exp(12,5)

8.

Suppose a program contains the following variable declarations:

int test1Score;

int test2Score;

Which of the following input statement is the correct statement to read two input values?

a)

cin << test1Score << test2Score;

b)

cin >> test1Score,test2Score;

c)

cin >> test1Score >> test2Score >> endl;

d)

cin >> test1Score >> test2Score;

9.

Which of the following is an invalid name for an identifier?

a)

7Length

b)

M2

c)

total7

d)

lengthTotal

10.

Write a C++ statement that declares the named constant PI which contains the value 3.14 and is a floating point constant.

(a)  

11.

Suppose a program contains the following variable declarations and assignments:


int test1Score = 90;

int test2Score = 80;

int sumOfScores = test1Score + test2Score;


Which of the following output statements will produce the output below (notice that the values stored in the variables have been output):

90 + 80 = 170

a)

cout << test1Score << + << test2Score << = << sumOfScores << endl;

b)

cout << test1Score + test2Score = sumOfScores << endl;

c)

cout << test1Score << " + "<< test2Score <<" = "<< sumOfScores << endl;

d)

cout << "test1Score" <<" + "<<" test2Score "<<" = "<< "sumOfScores" << endl;