CS201 Midterm Short Notes and Subjectives Question and Answer Danish Hanif
CS201 Midterm Short Notes and Subjectives Question and Answer Danish Hanif
The C++ programming language has a history going back to 1979, when Bjarne Stroustrup was
doing work for his Ph.D. thesis. One of the languages Stroustrup had the opportunity to work
with was a language called Simula, which as the name implies is a language primarily designed
for simulations.
Many programming languages and other computer files have a directive, often called include (as
well as copy and import), that causes the contents of a second file to be inserted into the original
file. These included files are called copybooks or header files.
Curly braces are used to group a set of statements. Often we use them along with loops and
conditional statements in order to avoid confusion and to define a clear scope.
In C, the "main" function is treated the same as every function, it has a return type (and in some
cases accepts inputs via parameters). The only difference is that the main function is "called" by
the operating system when the user runs the program.
Int or signed int unsigned int, short int or signed short int, long int or signed long int, unsigned
long int
The built in C++ library routines are kept in the standard namespace. That includes stuff like
cout, cin, string, vector, map, etc. Because these tools are used so commonly, it's popular to add
"using namespace std" at the top of your source code so that you won't have to type the std::
prefix constantly.
Size in Bytes Range int or signed int 2 -32,768 to 32767 unsigned int 2 0 to
65535 short int or signed short int 1 -128 to 127 long int or signed long int 4
4932 to 1.1E+4932
The Syntax of Declare variable is data_type variable_name; where the data_type is the type of
data which can hold a variable variable_name might be anything except the C directives and
reserve words.
Consider a situation, when we have two persons with the same name, Zara, in the same class.
Whenever we need to differentiate them definitely we would have to use some additional
information along with their name, like either the area if they live in different area or their
mother or father name, etc. Same situation can arise in your C++ applications. For example, you
might be writing some code that has a function called xyz() and there is another library available
which is also having same function xyz(). Now the compiler has no way of knowing which
version of xyz() function you are referring to within your code.A namespace is designed to
overcome this difficulty and is used as additional information to differentiate similar functions,
classes, variables etc. with the same name available in different libraries. Using namespace, you
can define the context in which names are defined. In essence, a namespace defines a scope.
namespacenamespace_name {
// code declarations
To call the namespace-enabled version of either function or variable, prepend the namespace
name as follows:
Reserve or Keywords are the words that C language use for his personal use a user cannot use
reserve words as variable, function or class name. Example: IF, Else, Switch, For, While, Float
etc.
3. Except underscore (_) no other special symbol are allowed in the middle of the variable
declaration.
5. Every variable name always should exist in the left hand side of assignment operator.
For a variable, a definition is a declaration which allocates storage for that variable.
Initialization is the specification of the initial value to be stored in an object, which is not
necessarily the same as the first time you explicitly assign a value to it.
cout is used for output, cin for input. Cout and cin are not key words in the C++ language. They
are variables, instances of classes that have been declared in <iostream>.
Variables that are declared inside a function or block are local variables. They can be used only
by statements that are inside that function or block of code. Local variables are not known to
functions outside their own.Global variables are defined outside of all the functions, usually on
top of the program. The global variables will hold their value throughout the life-time of your
program. A global variable can be accessed by any function. That is, a global variable is available
for use throughout your entire program after its declaration.
Asyntax error is an error in the syntax of a sequence of characters or tokens that is intended to be
written in a particular programming language. For compiled languages, syntax errors are
detected at compile-time. A program will not compile until all syntax errors are corrected.
An error that occurs during the execution of a program. In contrast, compile-time errors occur
while a program is being compiled. Runtime errors indicate bugs in the program or problems that
the designers had anticipated but could do nothing about. For example, running out of memory
will often cause a runtime error.
A logic error is a bug in a program that causes it to operate incorrectly, but not to terminate
abnormally (or crash). A logic error produces unintended or undesired output or other behavior,
although it may not immediately be recognized as such.
&& Called Logical AND operator. If both the operands are non-zero, then the condition
becomes true. (A && B) is false.
|| Called Logical OR Operator. If any of the two operands is non-zero, then the condition
becomes true. (A || B) is true.
! Called Logical NOT Operator. It is used to reverse the logical state of its operand. If a
condition is true, then Logical NOT operator will make it false. ! (A && B) is true.
A ternary operator or Conditional Operator is an operator that takes three arguments. The
arguments and result can be of different types. Many programming languages that use C-like
syntax feature a ternary operator, ?:, which defines a conditional expression. Since this operator
is often the only existing ternary operator in the language, it is sometimes simply referred to as
"the ternary operator". In some languages, this operator is referred to as "the conditional
operator". Another example for a ternary operator is between, as used in SQL and Python.
Operands are the objects that are manipulated and operators are the symbols that represent
specific actions. For example, in the expression. 5 + x. and 5 are operands and + is an operator.
All expressions have at least one operand.
ofstream This data type represents the output file stream and is used to create files and to
write information to files.
ifstream This data type represents the input file stream and is used to read information
from files.
In C programming Language it is often necessary to repeat the same block of code a given
number of times, or until a certain condition is met. Executes a sequence of statements multiple
times and abbreviates the code that manages the loop variable. You can use one or more loops
inside any other while, for, or do. While loop.
In C++ we can use if statement in the else block. Or we can also include if block in another if
block, it’s called nested IF. The placing of one loop inside the body of another loop is called
nesting. When you "nest" two loops, the outer loop takes control of the number of complete
repetitions of the inner loop.
While all types of loops may be nested, the most commonly nested loops are for loops.
The C preprocessor modifies a source file before handing it over to the compiler, allowing
conditional compilation with #ifdef, defining constants with #define, including header files with
#include.
This function use to copy string from one string variable to other string variable. Example
strcpy(s1, s2) copy string from s2 to s1.
The variable that stores the address of another variable is called a pointer. Pointers are a very
powerful feature of the language that has many uses in lower level programming.
Both are variables a pointer hold the memory address of a variable and an Variable that hold his
value both variable must be same type to work with each other.
A class in C++ is a user defined type or data structure declared with keyword class that has data
and functions (also called methods) as its members whose access is governed by the three access
specifies private, protected or public (by default access to members of a class is private).
Object is a class type variable. Objects are also called instance of the class. Each object contains
all members (variables and functions) declared in the class. We can access any data member or
member function from object of that class using. Operator.
40. How can we refer to the global variable if the local and the global variable names
are same?
We can apply scope resolution operator :: for the scope of global variable. The scope resolution
operator helps to identify and specify the context to which an identifier refers, particularly by
specifying a namespace. The specific uses vary across different programming languages with the
notions of scoping. In many languages the scope resolution operator is written "::".
An Identifier can only have alphanumeric characters (a-z, A-Z, 0-9) and underscore (_). The first
character of an identifier can only contain alphabet (a-z, A-Z) or underscore (_). Identifiers are
also case sensitive in C. For example name and Name are two different identifier in C.
Friend Function:A C++ friend functions are special functions which can access the private
members of a class. They are considered to be a loophole in the Object Oriented Programming
concepts, but logical use of them can make them useful in certain cases.
Friend Class: A C++ friend class is a special class that can access the members, objects and
methods of another class.
The parameters sent to the function at calling end are called as actual parameters while at the
If a header file is included with in <> then the compiler searches for the particular header file
Only with in the built in include path. If a header file is included with in “ “, then the compiler
Searches for the particular header file first in the current working directory, if not found then in
the built in include path
47. If a pointer is declared for a class, which operator can be used to access it?
Arrow operator (->) is used for accessing members of structure using pointer variable, whenever
we declare structure variable then member can be accessed using the dot operator. But when
pointer to a structure is used then arrow operator is used. Both dot and arrow operator serves
same function to access member of structure.
49. Is it possible to have Virtual Constructor? If yes, how? If not, Why not possible?
There is nothing like Virtual Constructor. The Constructor can’t be virtual as the constructor
Is a code which is responsible for creating an instance of a class and it can’t be delegated to any
other object by virtual keyword mean.
A dangling pointer arises when you use the address of an object after its lifetime is over. This
may occur in situations like returning addresses of the automatic variables from a function
orusing the address of the memory block after it is freed.
A smart pointer is a C++ class that mimics a regular pointer in syntax and some semantics, but it
does more. Because smart pointers to different types of objects tend to have a lot of code in
common, almost all good-quality smart pointers in existence are template by the pointe type.
A reference must always refer to some object and, therefore, must always be initialized; pointers
do not have such restrictions. A pointer can be reassigned to point to different objectswhile a
reference always refers to an object with which it was initialized.
53. What is the difference between const char *myPointer and char *const myPointer?
Const char *myPointer is a non-constant pointer to constant data; while char *const myPointer is
a constant pointer to non-constant data.