You are on page 1of 14

Name : Usama bin shahid

Roll no : FA20-BEE-086
Assignment : Introduction to computing
Submitted to : MR Usman Bashir
CHAPTER # 01
What is a compiler?

A compiler translates c++ source code into an executable machine code in compiler a new code is being compiled
into a target code.

How is compiled code different from source code?

 Source code is just a text .Text we type into the editor. Source code gets converted before it can run. It
cannot be executed natively by computers cpu. The higher level language code is called source code.
 A compiler convert the source code into a target code also known as compiled code. The compled code
may be the machine language for a particular platform or embedded device.

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

Compilers . A compiler is a program that lets your turn your source code file (the file in which you write your
program in c or c++) into an executable file.

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

Ans: A compiler translates c++ source code into an executable machine code in compiler a new code is being
compiled into a target code.

What does the linker do?

A linker combined the compiler-generated machine code with pre-compiled library code or compiled codefrom
other sources to make a complete executable program. It combines the programmer,s compiled code and the
library code to make a complete program.

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

The linker deals with the files containing machine language code which is being generated by the compiler .

What does the pre-processor do to source code?

Pre-processor add to or modifies the context of source code before the compiler begins processing the code. We
use the service of pre-processor mainly to #include information about library routines , or program use.

List several advantages developing software in a higher-level language has over developing software
in machine language.

1. High-level languages are programmer,s friendly. they are easy to write.debug and maintain.
2. They provide higher level of abstraction from machine languages.
3. They are machine independent languages.
4. Less error prone,easy to find and debug errors.
5. Easy to learn as compare to machine language.
6. Results in better programming productivity.

How can an IDE improve a programmer’s productivity?

An IDE enables programmers to consolidate the different aspects of writing a computer program IDE,s increases
programmer productivitlyby combinning common activities of writings software into a single application i.e-
editing source code,building executables and debugging.

Name a popular C++ IDE is used by programmers developing for Microsoft Windows.

(DEV C++)

Name a popular C++ IDE is used by programmers developing for Apple macOS.

GNAT programming studio.


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

#include <iostream>

This line in pre-processing directive,

What statement allows the short namecoutto be used instead ofstd::cout?

1. Using std::cout;
2. Using namespace std;

These allows usto write a short name for the std::cout printing object.

What does the name std stand for?

The name std stands for standard.

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

Int main() {

This specifies the real beginning of our program. All c++ programs must contain this function to be executable.

The body of main is enclosed within what symbols?

The body of main function is enclosed within opening only brace and closing curly brace.

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

Ans: (<<)

With output stream the insertion operator (<<) is used to put values in the stream this also make sense you insert
your values into the stream and the data consumer(moniter) user them.

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

1. #include<iostream>
2. Using namespace std
3. Int main() {
4. cout <<”usama”\n;
5. }

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.

1. #include<iostream>
2. Using namespace std
3. Int main() {
4. cout<<”usama”\n;
5. cout<<”shahid”\n;
6. }

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

It is possible other compiler has a framework version installed that matches therequirement of your software , you
can ship the application via creating on MSI with a step up or via sending the exe and any dlls required from
within the bin directory created during the compilation.

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

C++ like c and many other programming languages is free from we can break up statements across multiple lines a
long as we don’t separate the characters with in tokens. For example if your statement has an operator >>=, don’t
break up these three operators with a new line or any other white space character.

CHAPTER# 03
Will the following lines of code print the same thing? Explain why or why not.

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


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

ANS:a)The output of the statement will be 6

b)the output of the stament will be 6.

Will the following lines of code print the same thing? Explain why or why not. std::cout << x << '\n';
std::cout << "x" << '\n' ?

Ans:a)the output of the statament will be an error as variable is not declared

b)the output of the statement will be X.

What is the largestintavailable on your system?

Ans: largest available number is 2147483647

What is the smallestintavailable on your system?


Ans: smallest int available on system -32768

What is the largest double available on your system?

Ans: the largest double available on system is 1.8 x 10^308

What is the smallest doubleavailable on your system?

Ans: DBL_MINis the smallest positive normal double. DBL_TRUE_MIN is the smallest positive double.

What C++ data type represents nonnegative integers?

Ans : Unsigned integers are integers that can only hold non-negative whole numbers. A 1-byte unsigned
integer has a range of 0 to 255. Compare this to the 1-byte signed integer range of -128 to 127.

What happens if you attempt to use a variable within a program, and that variable is not declared?

Ans :An attempt to read the value of such an undeclared variable causes a runtime error.

What is wrong with the following statement that attempts to assign the value ten to variablex? 10 = x;

Ans: Syntax error

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

Ans: Once you declare a variable, the type of that variable cannot change.

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

Ans: int n[6];

In C++ can you declare more than variable in the same declaration statement? If so, how?

Ans: Though you can declare a variable multiple times in your C++ program, but it can be defined only once in a
file, a function or a block of code. Same concept applies on function declaration where you provide a function
name at the time of its declaration and its actual definition can be given anywhere else.

In the declaration int a; int b; do a and b represent the same memory location?

Ans: variables have been explained as locations in the computer's memory which can be accessed by their
identifier (their name). This way, the program does not need to care about the physical address of the data in
memory; it simply uses the identifier whenever it needs to refer to the variable.

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

(a) fred

legal

(b) if
illegal

(c) 2x

illegal

(d) -4

illegal

(e) sum_total

legal

(f) sumTotal

legal

(g) sum-total

illegal

(h) sum total

illegal

(i) sumtotal

legal

(j) While

illegal

(k) x2

legal

(l) Private

illegal

(m) public

illegal

(n) $16

illegal
(o) xTwo

legal

(p) _static

illegal

(q) _4

illegal

(r) ___

illegal

(s) 10%

illegal

(t) a27834

legal

(u) wilma's

illegal

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

Ans:  a reserved word (also known as a reserved identifier) is a word that cannot be used as an identifier, such as
the name of a variable, function, or label

WhydoesC++ requireprogrammerstodeclareavariablebeforeusingit? Whataretheadvantagesof


declaring variables?

Ans: C++ is a strongly-typed language, and requires every variable to be declared with its type before its first use.
This informs the compiler the size to reserve in memory for the variable and how to interpret its value.

The greatest advantage of the variables is that they enable one and the same program to execute various sets of
data. In the light of the afore-stated, a variable refers to a symbol for a varying value, which is stored in the
system's memory.

What is the difference between float and double?

Ans: 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 (32 bit) 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.

How can a programmer force a floating-point literal to be afloatinstead of adouble?


Ans: Use float if you have memory constraint because it takes almost half as much space as double. If your
numbers cannot fit in the range offered by float then use double. Though be careful with floating point calculation
and representation, don't use double or float for monetary calculation, instead use Big Decimal.

How is the value 2.45×10−5 expressed as a C++ literal?

Ans: Constants refer to fixed values that the program may not alter and they are called literals. Constants can be
of any of the basic data types and can be divided into Integer Numerals, Floating-Point Numerals, Characters,
Strings and Boolean Values.

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

Ans: Follow the As clause with an equal sign ( = ), followed by an expression. Be sure the compiler can evaluate
this expression to a constant value. You can assign a value to a ReadOnly variable only once. Once you do so, no
code can ever change its value.

How can you extend the range ofinton some systems?

Ans: 0 to 65,535. long. 4. long int , signed long int. -2,147,483,648 to 2,147,483,647.

How can you extend the range and precision ofdoubleon some systems?

Ans: If you’re extending both the range and precision of double precision floating point, I wouldn’t call it double
precision anymore. At least, not IEEE 754 double precision. Especially if you’re adding bits a la 80-bit extended
double precision.

So let’s limit ourselves to 64 bits. You could extend range OR precision by trading off mantissa and exponent bits:
more exponent gives more range, at the cost of precision, and vice versa.

Write a program that prints the ASCII chart for all the values from 0 to 127.

1. #include<iostream>
2. #include<iomanip>
3. using namespace std;
4.  
5. char const* character[] = {"", "", "", "", "", "", "", "",
6. "\\a","\\b","\\t","\\n","\\v","\\f","\\r", "",
7. "", "", "", "", "", "", "", "",
8. "", "", "", "", "", "", "", ""};
9.  
10. int main()
11. {
12. char c;
13. int row;
14. cout << " ASCII Table" << endl << "=============" << endl;
15. for(int i = 0; i < 16; i++)
16. {
17. row = i;
18. while (row <= 127) {
19. if (row < 32)
20. cout << setfill('0') << setw(2) << setbase(16)
21. << row << " = " << setw(3) << setfill(' ')
22. << character[i] << " | ";
23. else if (row >= 32 && row < 127)
24. {
25. c = row;
26. cout << setfill('0') << setw(2) << setbase(16)
27. << row << " = " << setw(3) << setfill(' ')
28. << c << " | ";
29. }
30. else
31. cout << setfill('0') << setw(2) << setbase(16)
32. << row << " = " << setw(3) << setfill(' ')
33. << "DEL" << " | ";
34. row = row + 16;
35. }
36. cout << endl;
37. }
38. }
39. cout << "!" << endl;
40. }

Is"i"a string literal or character literal?

Ans: A common convention for expressing a character literal is to use a single quote ( ') for character
literals, as contrasted by the use of a double quote (") for string literals.

Is'i'a string literal or character literal?

Ans: A common convention for expressing a character literal is to use a single quote ( ') for character
literals, as contrasted by the use of a double quote (") for string literals.

Is it legal to assign acharvalue to an int variable?

Ans: The character is only a representation of an integer value. For example, '0' can be written as 0x30 or
48 , 'a' is an alternative for 0x61 or 97 , etc. So the assignment is perfectly valid.

Is it legal to assign anintvalue to a char variable?

Ans: legal. In C character constant is of type int . Which makes it a bit different with C++.

What is printed by the following code fragment? int x; x = 'A'; std::cout << x << '\n';

Ans: A

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


Ans: You use '\n' if you are calling a function that expects a character, and "\n" , if it expects a string. '\n' is a char
literal. "\n" is a string literal (an array of chars, basically). The difference doesn't matter if you're writing it to an
ordinary stream.

Write a C++ program that simply emits a beep sound when run.

Ans:

#include <iostream>

#include <windows.h> // WinApi header

using namespace std;

int main()

Beep(523,500); // 523 hertz (C5) for 500 milliseconds

cin.get(); // wait

return 0;

Create an unscoped enumeration type that represents the days of the week.

Ans:

#include <iostream>
using namespace std;
// Define the  enum data type Days
enum Days
{
monday, //monday = 0 by default
tuesday = 0,//tuesday = 0 also
wednesday, //wednesday = 1
thursday, //thursday = 2
friday, //an so on.
saturday,
sunday
};
 
int main(void)
{
// Declaring the enum variable
// Try changing the tuesday constant, recompile and re run this program
enum Days WhatDay = tuesday;
 
switch (WhatDay)
{
case 0: cout<<"It's Monday"<<endl;
break;
default: cout<<"Other day"<<endl;
}
return 0;
}

Create a scoped enumeration type that represents the days of the week.

Ans:

#include <iostream>
Using namespace std;
int main()
{
enum class Week
{
Sunday,
// Sunday is inside the scope of Week
Monday,
Tuesday
};
Week week = Week::Monday;
// compile error here, as the compiler doesn't know //how to compare different types Month and Week
cout << "Month and Week are not same\n";
return 0;
}

Create an unscoped enumeration type that represents the months of the year.

#include <iostream>
using namespace std;
int main()
{
enum month { Jan, Feb, Mar, April, May, Jun, July, Aug, Sep, Oct, Nov, Dec };
month M;

return 0;
}

Create a scoped enumeration type that represents the months of the year.

Ans: #include <iostream>


using namespace std;
int main()
{
enum class Month
{
Jan,
Feb,
Mar
};
Month month = Month::Feb;
if (month == Month::Feb) // this is okay
cout << "Month is February!\n";
else if (month == Month::Jan)
cout << "Month is January!\n";
return 0;
}

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