Vous êtes sur la page 1sur 5

Keerthi Venkataramani

Exercise 3
1.
2.
3.
4.

OOP is an acronym that means Object Oriented Programming. TRUE


C++ not only supports OOP but also supports other programming styles. TRUE
In C++ the variables Alpha, ALPHA and AlphA are the same identifier. FALSE
In C++ the compiler will infer the type intended for a variable from the context in which
the variable occurs. TRUE
1.
2.

A C++ declaration introduces only an identifier's spelling and specifies its type.
TRUE
A C++ declaration is a definition that also allocates storage for an identifier's value
(or function's body etc.). TRUE
1.

The range of values for an int variable is from about 0 to +2 billion. TRUE

2.

In C++ you can assign an expression of type int to a variable of type double with
no problem. TRUE

3.

In C++ you can assign an expression of type double to a variable of type int with
no problem. FALSE

4.

To put a character into a cstring constant that causes the output to continue on
the next line, insert the escape sequence \t into the string constant. FALSE
1.

If we execute this code in an otherwise correct and complete program:

n = 1;
n = (n++) + (n++);
the value of n is guaranteed to be 3 after the second line executes. FALSE
1.
2.

C++ uses only /* */ for comments. FALSE


A programs comments should connect the program code to the problem being solved.

Answer: True.
1.
2.
3.

A program should have a comment on every line. FALSE


Comments have no value whatsoever and do not belong in a program. TRUE
A computer program is a set of instructions intended for only the computer to follow.
TRUE

2. Multiple Choice
1. The person responsible for the initial development of C++ was

d)

Bjarne Strousrup

1.

b)

In C++, some of the following are legal identifiers. Which? Why?

Xyz

d) xy_z
1.

Which of the following are likely to be poor choices for an identifier in a program?
Why?

a)x
d)

_abc

e)A
1.

Pick the C++ keywords out of the following list.

a)

while

c)

double

d)

if

1.

a)
1.

b)
1.

Before a variable in C++ is used, it must be

defined
Which of the following types are not built into the C++ language:

real
Which of the following names might give the human reader some hint about the data
that is stored in them?

b)

speed, distance, time

c)

hypotenuse, leg1, leg2

e)

principal, interest, payment

1.

c)
1.

b)
1.

The value of the expression 20.0 * (9/5) + 32.0 is

incorrect expression so there is no value


The value of the expression 20.0 * (9.0/5) + 32.0 is

52.0
The following contain several #include directives that may have problems. Which have
one or more problems, and what problems?

b)

#include < iostream> there is a space

d)

#include cctype quotation is backward

e)

#include This file does not exist on the system doesnt make sense

3. Free Form Questions:


1.

Write a C++ program that outputs "My first C++ program" and then
outputs a carriage return.

#include "stdafx.h"
#include<iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
cout << "My First C++ Program" << '\r';
system("pause");
return 0;
}

2.

Given the C++ output statements. What is the output of these lines of
code? Explain.

cout << "If you have ";


cout << "a number of pods ";
cout << "you can quit.";
cout << "\n";
The output of this code states on one line that If you have a number of pods you can
quit
1.

What does the line

#include <iostream>
do for your program?
It allows the program to be compiled and gives access to a library.
1.

What is the difference between a warning from the compiler and an


error message from the compiler? Error means that the syntax is wrong
and must be fixed in order to work. Warning means there might be an
error so the code should be reworked to prevent crashing

2.

What is the value assigned to the variable in each of these cases?


Explain curious results. Be careful!

int x, y;
a)

x = 1/2; 0

b)

y = 3.0/2.0; 1

double z, w, t;
c)

z = 1/2; 0

d)

w = 3/2; 1

e)

t = 3.0/2.0; 1.5

4. Write a program to calculate the area of a square


//Exercise 3
//
#include "stdafx.h"
#include<iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
double length, area;
cout << "Enter length\n";
cin >> length;
area = length*length;
cout << "area is " << area << '\n';
system("pause");
return 0;
}

4. Write a program to calculate the commission of a salesperson. The commission


is 10% of the sales. The user input the value of the sales.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.

//Exercise 3
//
#include "stdafx.h"
#include<iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
double sales, commission;

16.
17.
18.
19.
20.
21.
22. }

cout << "Enter amount of sales\n";


cin >> sales;
commission = 0.1*sales;
cout << "commission is " << commission << '\n';
system("pause");
return 0;