Muhammad Rizwan

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Name: Muhammad Rizwan

Roll number: 073

Class: 1st semester – section B

Chapter #1 Exercise Questions

Q1: What is a compiler?

A Compiler translates the source code to target code. The target code may be the machine
language for a particular platform or embedded device.

Q2: How is compiled code different from source code?

As source code is an input to the compiler by a programmer writer.A compiler just compiles the
source code and gives us the output. The difference is that source code is the input through
programmer while compiled code is the output by the compiler.

Q3: What tool does a programmer use to produce C++ source code?

The tools used by the programmers to produce C++ source code are: 1. Reprocessor 2. Compiler
3.Linkers

Q4: What tool(s) does a programmer use to convert C++ source code into executable machine
code?

The tool used to convert C++ source code into executable machine code is a Compiler.

Q5: What does the linker do?

A program called a linker combines the programmer’s compiled code and the library code.

Q6: Does the linker deal with files containing source code or machine language code?

Yes,It deals with the files containing source code or machine language code.

Q7: What does the preprocessor do to source code?

Preprocessor adds or modifies the contents of source file before the compiler begins processing
the code.

Q8: List several advantages of developing software in a higher-level language has over
developing software in machine language?

High level languages are programmer friendly and it provides higher level of abstraction. It is
machine independent language, very easy to learn, less error prone, easy to find and debug
errors.

Q9: How can an IDE improve a programmer’s productivity?


Many developers use integrated development environments. An IDE includes editors,
debuggers, and other programming aids in one comprehensive program.

Q10: Name a popular C++ IDE is used by programmers developing for Microsoft Windows?

Visual Studio is used by programmers developing for Microsoft Windows.

Q 11:Name a popular C++ IDE is used by programmers developing for Apple macOS?

XCode is a popular IDE used by programmers.

Chapter #2 Exercise Questions

Q1: What preprocessor directive is necessary to use statements with the std::cout printing
stream object?

The std:: before cout is required when we use names that we've brought into the program by
the preprocessor directive #include.

Q2: What statement allows the short name cout to be used instead of std::cout?

The statement Using allows us to use a shorter name for the std::cout printing object. We can
omit the std:: prefix and use the shorter name, cout.

Q3: What does the name std stand for?

The name std stands for “standard” and the std prefix indicates that cout is a part of collection
of names called the standard namespace.

Q4: All C++ programs must have a function named what?

Main is the function that all C++ programs must contain.

Q5: The body of main is enclosed within what symbols?

The body of main is enclosed within curly braces{}.

Q6: What operator directs information to the std::cout output stream?

The symbols << make up the insertion operator. You can think of the message to be printed as
being “inserted” into the cout object.

Q7: Write a C++ program that prints your name in the console window.

#include <iostream>

int main () {

std::cout << “Muhammad Rizwan Jutt\n”;

}
Q8: Write a C++ program that prints your first and last name in the console window. Your first
name should appear on one line, and your last name appear on the next line?

#include <iostream>

int main () {

std::cout << “Muhammad\n”;

std::cout << “Rizwan\n”;

Q9: What other files must you distribute with your executable file so that your program will run
on a Windows PC without Visual Studio installed?

C++ programmers have two options for C++ development environments. One option involves a
command-line environment with a collection of independent tools and the other is IDE.

Q10: Can a single statement in C++ span multiple lines in the source code?

Yes. C++, like C and many other programming languages, is free-form.

Chapter #3 Exercise Questions

Q1: Will the following lines of code print the same thing? Explain why or why not.

std::cout << 6 << '\n';


std::cout << "6" << '\n';

Yes, they will print the same thing ,since 6 has no character type assigned to it in ASCII system.

Q2: Will the following lines of code print the same thing? Explain why or why not.

std::cout << x << '\n';


std::cout << "x" << '\n';

std::cout << x << '\n';


prints 10, the value of the variable x, but the statement
std::cout << "x" << '\n';
prints x, the message containing the single letter x.

Q3: What is the largest int available on your system?

2,147,483,647 the largest int available on our system.

Q4: What is the smallest int available on your system?

- 2,147,483,648 is the smallest int available on our system.


Q5: What is the largest double available on your system?

1.79769*10+308 is the largest double available on our system.

Q6: What is the smallest double available on your system?

2.22507*10-308 is the smallest double available on our system.

Q7: What C++ data type represents nonnegative integers?

C++ provides integer-like types that exclude negative numbers. These types include the word
unsigned in their names, meaning they do not allow a negative sign.
Q8: What happens if you attempt to use a variable within a program, and that variable is not
declared?

If you failed to declare variable, then an ERROR will be thrown by the compiler. If you failed to
define a value to variable, then compiler will throw a warning mess. If
you are using any variable inside code, then declaring a variable is must.

Q9: What is wrong with the following statement that attempts to assign the value ten to
variable x?

10 = x;

An integer can’t be written before a variable, so an error will be generated in this case.

Q10: Once a variable has been properly declared and initialized can its value be changed?

Once declared, a particular variable cannot be redeclared in the same context. A variable may
not change its type during its lifetime.

Q10: What is another way to write the following declaration and initialization?
int x = 10;

#include <iostream>

int main() {

int x{10};

std::cout << x << '\n';

}
This alternate form is not commonly used for simple variables, but it necessary for initializing
more complicated kinds of variables called objects.

Q12: In C++ can you declare more than one variable in the same declaration statement? If so,
how?
Variables can be reassigned different values as needed, such as,
#include <iostream>
int main() {
int x;
x = 10;
std::cout << x << '\n';
x = 20;
std::cout << x << '\n';
x = 30;
std::cout << x << '\n';
}

Q13: In the declaration

int a;
int b;

do a and b represent the same memory location?

No, more importantly, the statement

int a;
int b;

does not mean a and b refer to the same box (memory location).

Q14: Classify each of the following as either a legal or illegal C++ identifier:

fred Legal
if Illegal
2x Illegal
-4 Illegal
sum_total Legal
sumTotal Legal
sum-total Illegal
sum total Illegal
sumtotal Legal
While Legal
x2 Legal
Private Legal
public Illegal
$16 Illegal
xTwo Legal
_static Illegal
_4 Illegal
___ Illegal
10% Illegal
a27834 Legal
wilma's Illegal

Q15: What can you do if a variable name you would like to use is the same as a reserved word?

When variables must be declared, the compiler can catch typographical errors that dynamically-
typed languages cannot detect. For example, consider the following section of code:
char While;
While = 1;

Q16: Why does C++ require programmers to declare a variable before using it? What are the
advantages of declaring variables?

Before they are used, all variables have to be declared. Declaring a variable means defining its
type, and optionally, setting an initial value (initializing the variable). Variables do not have to
be initialized (assigned a value) when they are declared, but it is often useful.

Q17: What is the difference between float and double?

The Decimal, Double, and Float variable types are different in the way that they store the
values. Precision is the main difference where float is a single precision (32bit) floating point
data type, double is a double precision (64 bit) floating point data type and decimal is a 128-
bit floating point data type.

Q18: How can a programmer force a floating-point literal to be a float instead of a double?

To make a literal floating-point value a float, you must append an f or F to the number, as in
3.14f (The f or F suffix is used with literal values only; you cannot change a double variable into
a float variable by appending an f. Attempting to do so would change the name of the variable!)

Q19: How is the value 2.45*10-5 expressed as a C++ literal?

It is expressed as 2.45e-5.

Q20: How can you ensure that a variable’s value can never be changed after its initialization?

Constants are declared like variables with the addition of the const keyword:
const double PI = 3.14159;
Once declared and initialized, a constant can be used like a variable in all but one way—a
constant may not be reassigned. It is illegal for a constant to appear on the left side of the
assignment operator (=) outside its
declaration statement. A subsequent statement like
PI = 2.5;
would cause the compiler to issue an error message.
Q21: How can you extend the range of int on some systems?

We can increase the range of int by adding longh and long long before int like:

long int;

long long int;

Q22: How can you extend the range and precision of double on some systems?

We can extend the range and precision of double by adding long before double like:

long double;

It almost doubles the range and precision of double.

Q24: Is "i" a string literal or character literal?

It is a string literal.

Q25: Is 'i' a string literal or character literal?

It is a character literal.

Q26: Is it legal to assign a char value to an int variable?

If the numeric value is in between 0 to 127, then we can assign char value to the int value
according to ASCII system.

Q27: Is it legal to assign an int value to a char variable?

If the character is from the ASCII system, then we can assign int value to char value according to
the ASCII system.

Q28: What is printed by the following code fragment?

int x;
x = 'A';
std::cout << x << '\n';

Ans: It prints int 65 acording to ASCII system, which is A’s value.

Q29: What is the difference between the character 'n' and the character '\n'?

'n' is just a character, whereas '\n' is used to end a line.

Q31: Create an unscoped enumeration type that represents the days of the week.
#include <iostream>
using namespace std;
enum Days
{
monday,
tuesday,
wednesday,
thursday,
friday,
saturday,
sunday
};
}

Q35: Determine the exact type of each of the following variables:

(a) auto a = 5; int


(b) auto b = false; bool
(c) auto c = 9.3; double
(d) auto d = 5.1f; float
(e) auto e = 5L large int

You might also like