1) What language does the CPU understand ?
2) What is the device that stores the program that the CPU works with.
3) We have a program spread over 3 files "main.cpp" , "printme.cpp" , "printme.h" . The file "printme.cpp" contains the definition for the function "printme".
The program is compiled using the below commands.
g++ -c printme.cpp
g++ -c main.cpp
g++ -o main.exe printme.o main.o
Explain the compilation process. Talk about the preprocessor, object files and executable files.
4) Program:
#include <iostream>
using namespace std;
int main()
{
int entry;
cout << "Write a number: ";
cin >> entry;
cout << entry;
cin.get();
return 0;
}
Sample Output:
[amittal@hills InputOutput]$ ./a.out
Write a number: 12
12[amittal@hills InputOutput]$
The above program was written with the intention that the program prompts the user to input a number and after the user inputs the number then the user waits for another input from the user. However when we run the program the program does not wait for the second input. Explain what is wrong with the program.
5) The below program converts Kg to pounds . Arrange the lines in the below program so that the program works correctly.
int main()
{
cin >> kg ;
cout << kg << " kg is " << (kg * 2.2 ) << " pounds" << endl ;
cout << "Please enter a value in Kg:" ;
return(0) ;
#include <iostream>
using namespace std ;
}
float kg ;
6) What is printed by the below program ?
#include <iostream>
using namespace std ;
int main()
{
int x1 , x2, y1 ;
x1 = x2 = y1 = 5 ;
x1 = x2 * y1 ;
x1 += 3 ;
x2 = x1 % 3 ;
cout << x1 << " " << x2 << " " << y1 << endl ;
return ( 0 ) ;
}
7) What is printed by the below program ?
#include <iostream>
#include <cmath>
using namespace std ;
int main()
{
int x1 , x2, y1 ;
x1 = 5 ;
x2 = 4 ;
y1 = 11 ;
x1 = (int)(pow( x2 , 2.0 )) % x1 ;
x2 = x1 / y1 ;
cout << x1 << " " << x2 << " " << y1 << endl ;
return ( 0 ) ;
}
8) What is printed by the following program ?
#include <iostream>
#include <string>
using namespace std ;
int main()
{
char ch1 ;
ch1 = 'A' ;
ch1 += 1 ;
string str1 = "review" ;
cout << ch1 << " " << str1 << " " << str1.length() << endl ;
return ( 0 ) ;
}
9) What will the below program print ?
#include <iostream>
using namespace std ;
int main()
{
int number1 , number2 ;
number1 = 5 ;
number2 = 10 ;
cout << "Step0\n";
if ( number1 < number2 )
{
cout << "Step1\n";
if ( (number2-number1) > 10)
{
cout << "Step2\n";
if (number1 > 20)
{
cout << "Step3\n";
}
}
else if ( (number2-number1) <= 10)
{
if ( number2 > 15 )
cout << "Step4\n" ;
else if ( number2 > 5 )
cout << "Step5\n" ;
else
cout << "Step6\n" ;
}
}
else
{
cout << "Step7\n";
if ( (number2-number1) > 10)
{
cout << "Step8\n";
if (number1 > 20)
{
cout << "Step9\n";
}
}
else if ( (number2-number1) <= 10)
{
if ( number2 > 15 )
cout << "Step10\n" ;
else if ( number2 > 5 )
cout << "Step11\n" ;
else
cout << "Step126\n" ;
}
}//if ( number1 < number2 )
cout << "Step13\n" ;
return(0) ;
}
10) What will the below program print ?
#include <iostream>
using namespace std ;
int main()
{
for( int i1=2 ; i1 < 8 ; i1 += 2 )
for( int j1 = i1 ; j1 < (i1+2) ; j1++ )
{
cout << (i1+j1) << endl ;
}
return(0) ;
}
1)
What does the below print ?
#include <iostream>
using namespace std ;
int main()
{
int arr1[] = { 1 , 2, 3, 4 , 5 } ;
int arr2[5] = { 1 , 2 } ;
for( int i1=0 ; i1 < 5 ; i1++ )
{
arr2[i1] = arr1[i1] + arr2[i1] ;
arr2[i1] += arr1[i1] ;
cout << arr2[i1] << " " << endl ;
} //for
return(0) ;
}
2)
What does the following print ?
#include <iostream>
#include <vector>
using namespace std ;
int main()
{
vector<int> vectorObj ;
vectorObj.push_back( 2 ) ;
vectorObj.clear() ;
vectorObj.push_back( 3 ) ;
vectorObj.push_back( 4 ) ;
vectorObj[1] = 25 ;
vectorObj.pop_back() ;
vectorObj.push_back( 5 ) ;
for( int i1=0 ; i1< vectorObj.size() ; i1++ )
{
cout << vectorObj[i1] << " " ;
}
return(0) ;
}
3)
What does the below program print ?
#include <iostream>
#include <vector>
using namespace std ;
int main()
{
int dim2[2][4] = { { 1, 2, 3 ,4} , { 5, 6, 7 ,8} } ;
for( int i1=0 ; i1<2 ; i1++ )
{
for( int j1=0 ; j1<4 ; j1 +=2 )
{
cout << dim2[i1][j1] << " " ;
}
cout << endl ;
}
return(0) ;
}
4)
What does the following print ?
#include <iostream>
using namespace std ;
int main()
{
int x1 = 100 ;
int y1 = 200 ;
int* ptr1 = &x1 ;
int* ptr2 = &y1 ;
cout << *ptr1 << " " << *ptr2 << endl ;
*ptr1 = *ptr2 ;
cout << *ptr1 << " " << *ptr2 << endl ;
cout << x1 << " " << y1 << endl ;
return(0) ;
}
5)
What does the following print ?
#include <stdio.h>
int main()
{
int MyArray[3] = { 10, 20, 30 };
int *ptr = MyArray;
*ptr = 70;
ptr += 1;
*ptr = 60;
ptr += 1;
*ptr = 50;
ptr += 1;
do
{
ptr--;
printf( "%d\n", *ptr ); // print values
} while ( ptr > MyArray );
}
6)
What will the below print ?
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std ;
int main()
{
char name[10] ;
char str1[50] ;
int length ;
strcpy(name, "Jimmy") ;
length = strlen(name) ;
cout << "length=" << length << endl ;
strcat( str1, name ) ; strcat( str1, "Masters") ;
length = strlen(str1) ;
cout << "length=" << length << endl ;
string str2( str1 ) ;
//str2 = "Test" ;
string str3 = str2 + str2 ;
cout << "length=" << str3.length() << endl ;
return (0 ) ;
}
7)
#include <iostream>
using namespace std ;
class Demo
{
public:
int x1 ; int x2 ;
Demo(int x1p, int x2p)
{
x1 = x1p ;
x2 = x2p ;
cout << "Inside the constructor." << endl ;
cout << "x1:" << x1 << " x2:" << x2 << endl ;
}
};
int main()
{
cout << "Before creating the object." << endl ;
Demo demo1Obj ( 2,5 ) ;
cout << "After creating the object." << endl ;
Demo demo2Obj ( 3, 6 ) ;
}
8)
#include <iostream>
#include <string>
using namespace std;
//-------------------------------------------------------------
class Person
{
public:
string firstName ;
string lastName ;
int age ;
public:
void print()
{
cout << firstName << " : " << lastName << ": " << age << endl ;
}
Person()
{
age = 42 ;
firstName = "Sunny" ;
lastName = "Liston" ;
//cout << "Person constructor with no arguments." << endl ;
}
Person( int ageP )
{
age = ageP ;
firstName = "Jack" ;
lastName = "Johnson" ;
//cout << "Person constructor with single argument." << endl ;
}
Person(int ageP, string firstNameP,
string lastNameP)
{
age = ageP ;
firstName = firstNameP ;
lastName = lastNameP ;
//cout << "Person Constructor with arguments.\n" ;
}
};
//-------------------------------------------------------------
int main()
{
Person arrayOfObjects[3] = { Person(20, "Joe", "Louis") , 52 } ;
for( int i1=0 ; i1 < 3 ; i1++ )
{
arrayOfObjects[i1].print() ;
} //for
return(0) ;
}
//-------------------------------------------------------------
Output:
[amittal@hills final]$ ./a.out
Joe : Louis: 20
Jack : Johnson: 52
Sunny : Liston: 42
9)
What does the following output ?
#include <iostream>
#include <string>
using namespace std ;
class Demo
{
public:
string str ;
Demo(string str1)
{
str = str1 ;
cout << "Constructor for " << str1 << endl ;
}
Demo(const Demo& demoObject )
{
str = demoObject.str ;
cout << "Copy Constructor for " << str << endl ;
}
void operator=( Demo& demo2 )
{
str = demo2.str ;
cout << "Assignment Operator for " << str << endl ;
}
};
int main()
{
Demo demo1("demo1") ;
Demo demo2("demo2") ;
Demo demo3( demo1 ) ;
demo1 = demo2 ;
return ( 0 ) ;
}
10)
What does the following print ?
#include <iostream>
using namespace std ;
class Base
{
public:
void function1()
{
cout <<"Inside function1 in the Base class." << endl ;
}
};
class Derived1 : public Base
{
public:
virtual void function1()
{
cout <<"Inside function1 in the Derived1 class." << endl ;
}
};
class Derived2 : public Derived1
{
public:
void function1()
{
cout <<"Inside function1 in the Derived2 class." << endl ;
}
};
int main()
{
Derived2 Derived2Object ;
Base baseObject = Derived2Object ;
Derived1 derived1Object = Derived2Object ;
baseObject.function1() ;
derived1Object.function1() ;
Derived2Object.function1() ;
Derived2* ptrDerived2Object = new Derived2() ;
Base* ptrbaseObject = ptrDerived2Object ;
Derived1* ptrderived1Object = ptrDerived2Object ;
ptrbaseObject->function1() ;
ptrderived1Object->function1() ;
ptrDerived2Object->function1() ;
}
1)
We have 2 arrays :
int arr1[] = { 1 , 2, 3, 4 , 5 } ;
int arr2[5] = { 1 , 2 } ;
The "arr2" is partially initialized and that means the rest of the elements ( 2,3,4 ) get set to 0 .
The for loop iterates through 0 , 1 , 2 , 3, 4 . Each time
the following lines are executed.
arr2[i1] = arr1[i1] + arr2[i1] ;
arr2[i1] += arr1[i1]
To simplify the steps we shall work on the whole arrays at once.
In the first line the contents of "arr1" and "arr2" are added and assigned to "arr2" and so we get:
1 , 2 ,3 ,4 ,5
1, 2, 0, 0, 0
-----------------
2, 4, 3, 4 , 5
In the next line we add the contents of "arr1" are added again to "arr2"
1 , 2, 3, 4, 5
2, 4, 3, 4 , 5
---------------
3, 6, 6, 8, 10
The answer is 3 , 6, 6, 8, 10 .
2)
Vector is a container from the Standard Template Library .A vector can store elements in a sequence and has the ability to grow as
elements are added.
Operations are:
push_back() Adds an element to the end of the container.
clear() Clears out all the contents
pop_back() Remove the last element .
size() returns the number of the elements in the container.
[] Subscript returns the element at the position indicated by the subscript index.
#include <iostream>
#include <vector>
using namespace std ;
int main()
{
vector<int> vectorObj ;
vectorObj.push_back( 2 ) ;
//Vector now contains
2
vectorObj.clear() ;
//Contents are cleared out
vectorObj.push_back( 3 ) ;
//Vector now contains
3
vectorObj.push_back( 4 ) ;
//Vector now contains
3 4
vectorObj[1] = 25 ;
//The element at position 1 ( second position is set to 25 .
Contents are now
3 25
vectorObj.pop_back() ;
Contents are now
3
vectorObj.push_back( 5 ) ;
Contents are now :
3 5
for( int i1=0 ; i1< vectorObj.size() ; i1++ )
{
cout << vectorObj[i1] << " " ;
}
return(0) ;
}
3 5 is printed out.
3)
int main()
{
int dim2[2][4] = { { 1, 2, 3 ,4} , { 5, 6, 7 ,8} } ;
for( int i1=0 ; i1<2 ; i1++ )
{
for( int j1=0 ; j1<4 ; j1 +=2 )
{
cout << dim2[i1][j1] << " " ;
}
cout << endl ;
}
return(0) ;
The 2 dimensional array can be pictured as:
cols 0 1 2 3
row 0 : 1 2 3 4
row 1: 5 6 7 8
For the outer loop i1 goes from 0 to 1 . For the inner loop "j1" is incremented from 0 to 2 ( increment by 2 every time by j1+=2 )
i1= 0
j1 0 2
We have 0,0 and 0,2
i1=1
j1 0 2
We have 1,0 and 1,2
So we have the following printed out:
1 , 3
5 , 7
4)
int main()
{
int x1 = 100 ;
int y1 = 200 ;
int* ptr1 = &x1 ;
int* ptr2 = &y1 ;
cout << *ptr1 << " " << *ptr2 << endl ;
*ptr1 = *ptr2 ;
cout << *ptr1 << " " << *ptr2 << endl ;
cout << x1 << " " << y1 << endl ;
return(0) ;
}
ptr1 contains the address of x1
ptr2 contains the address of y1
cout << *ptr1 << " " << *ptr2 << endl ;
Will print 100 200
since the *ptr1 will print the value at the address of x1 and y1 for ptr2.
*ptr1 = *ptr2 ;
This is changing the value of x1 since *ptr1 is on the left hand side.
cout << *ptr1 << " " << *ptr2 << endl ;
cout << x1 << " " << y1 << endl ;
Will print
200 200
200 200
5)
#include <stdio.h>
int main()
{
int MyArray[3] = { 10, 20, 30 };
int *ptr = MyArray;
*ptr = 70;
ptr += 1;
*ptr = 60;
ptr += 1;
*ptr = 50;
ptr += 1;
do
{
ptr--;
printf( "%d\n", *ptr ); // print values
} while ( ptr > MyArray );
}
The address of MyArray is assigned to "ptr" .
Then
*ptr = 70 and the array is now
70, 20,30
ptr is incremented
*ptr=60 produces
70 60 30
ptr is incremented and
*ptr = 50 . Contents of array are now
70 60 50
and ptr points to one element past 50
We enter the do loop
ptr--
Pointer is now pointing to 50
50 gets printed out
MyArray points to address at 70
We enter the while loop again and "ptr--"
Now pointer points to 60 and 60 gets printed out.
ptr > MyArray is still true as ptr points to 60 and MyArray points to 50
ptr gets decremented and 70 gets printed out .
Now ptr equals MyArray and the while condition is false and we come out of the loop.
Output: 50 60 70
6)
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std ;
int main()
{
char name[10] ;
char str1[50] ;
int length ;
strcpy(name, "Jimmy") ;
//Copies Jimmy to the array name
length = strlen(name) ;
cout << "length=" << length << endl ;
//Length is 5
strcat( str1, name ) ;
str1 now contains the contents of name which is "Jimmy"
strcat( str1, "Masters") ;
The string "Masters" is appended to str1 . So now str1 contains
"JimmyMasters"
length = strlen(str1) ;
cout << "length=" << length << endl ;
This prints the length of contents of str1 which is 12
string str2( str1 ) ;
//str2 is initialized with contents of str1 and str2 now contains
"JimmyMasters"
//str2 = "Test" ;
string str3 = str2 + str2 ;
//str3 contains
"JimmyMastersJimmyMasters"
cout << "length=" << str3.length() << endl ;
This prints 24
return (0 ) ;
7)
#include <iostream>
using namespace std ;
class Demo
{
public:
int x1 ; int x2 ;
Demo(int x1p, int x2p)
{
x1 = x1p ;
x2 = x2p ;
cout << "Inside the constructor." << endl ;
cout << "x1:" << x1 << " x2:" << x2 << endl ;
}
};
int main()
{
cout << "Before creating the object." << endl ;
Demo demo1Obj ( 2,5 ) ;
cout << "After creating the object." << endl ;
Demo demo2Obj ( 3, 6 ) ;
}
Constructor is called when the object is created. If objects are created on the stack in an order then they are destroyed in the reverse order.
We enter "main" and
Before creating the object
demo1Obj( 2, 5 ) prints
Inside the constructor
x1:2x2:5
Then
After creating the object
is printed
Then the constructor for demo2Obj is printed
x1:3x2:6
is printed
8)
The Person class has 3 constructors
Person()
{
age = 42 ;
firstName = "Sunny" ;
lastName = "Liston" ;
//cout << "Person constructor with no arguments." << endl ;
}
Person( int ageP )
{
age = ageP ;
firstName = "Jack" ;
lastName = "Johnson" ;
//cout << "Person constructor with single argument." << endl ;
}
Person(int ageP, string firstNameP,
string lastNameP)
{
Person arrayOfObjects[3] = { Person(20, "Joe", "Louis") , 52 } ;
We have an array of 3 objects . The first object is constructed with name "Joe" "Louis" and age 20. The second constructor is created with
name "Jack Johnson" and age 52. The last object will be the object with the default (empty) constructor and the name "Sunny" "Liston" with age 42 is assigned to the object.
So the print statement in the loop will print:
Joe : Louis: 20
Jack : Johnson: 52
Sunny : Liston: 42
9)
class Demo
{
public:
string str ;
Demo(string str1)
{
str = str1 ;
cout << "Constructor for " << str1 << endl ;
}
Demo(const Demo& demoObject )
{
str = demoObject.str ;
cout << "Copy Constructor for " << str << endl ;
}
void operator=( Demo& demo2 )
{
str = demo2.str ;
cout << "Assignment Operator for " << str << endl ;
}
};
int main()
{
Demo demo1("demo1") ;
//Creates the demo1 object
prints
Constructor for demo1
Demo demo2("demo2") ;
prints
Constructor for demo2
Demo demo3( demo1 ) ;
//The above invokes the copy constructor and the string of demo1 is assigned to demo3
Copy constructor for demo1
demo1 = demo2 ;
The above invokes the assignment operator.
Assignment Operator for demo2
return ( 0 ) ;
}
10)
The class Derived2 is derived from Derived1 and Derived1 is derived from class Base . All the classes have an overloaded function named "function1" .
A derived class object can be assigned to it's base class. A virtual function means if a derived class is cast as a pointer to the base class pointer and the
base class function is called then the overloaded function in the derived class will get called. This polymorphism behavior only starts in the class that has the virtual function defined. A derived class does not need the virtual keyword if it is defined somewhere in one of the base classes.
Virtual functions does not apply to objects that are not pointers.
baseObject.function1() ;
//Prints
Inside function1 in the Base class
derived1Object.function1() ;
//Prints
Inside function1 in the Derived1 class
Derived2Object.function1() ;
//Prints
Inside function1 in the Derived2 class
Derived2* ptrDerived2Object = new Derived2() ;
Base* ptrbaseObject = ptrDerived2Object ;
Derived1* ptrderived1Object = ptrDerived2Object ;
ptrbaseObject->function1() ;
//Prints
Inside function1 in the Base class
Virtual has not been defined in the hierarchy yet.
//Virtual has been defined and the actual object stored inside ptrderived1Object is of type Dervied2 so it prints
ptrderived1Object->function1() ;
Inside function1 in the Derived2 class
ptrDerived2Object->function1() ;
This also prints
Inside function1 in the Derived2 class