For the coding questions explain your reasoning behind your answer.
1)
Explain the C++ compiler process. Define preprocessor, object file, executable file, compiler , linker.
2)
What does the following print ?
#include <iostream>
using namespace std;
int main()
{
int x1 = 5 ;
int y1 = 0 ;
if ( x1 = y1 )
cout << "Values are equal." << endl ;
if ( x1 == y1 )
cout << "Values are equal." << endl ;
else if ( x1 <= y1 )
cout << "Values are not equal." << endl ;
}
3)
What does the following print ?
#include <iostream>
using namespace std;
int value = 100;
int main()
{
int value = 10 ;
cout << value << endl ;
{
int value = 20 ; //
cout << value << endl ;
{
int value = 30 ; //
cout << value << endl ;
} //A
cout << value << endl ;
} //B
cout << value << endl ;
cout << ::value << endl ;
return 0;
}
4)
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 ;
5)
What does the following print ?
#include <iostream>
using namespace std;
int main()
{
char ch ;
ch = 'A' + 2 ;
cout << "Step1" << endl ;
switch( ch )
{
case 'A' :
case 'a' :
cout << "Step2" << endl ;
break ;
case 'B' :
case 'b' :
cout << "Step3" << endl ;
break ;
case 'C' :
case 'c' :
cout << "Step4" << endl ;
default:
cout << "Step5" << endl ;
}
cout << "Step6" << endl ;
return 0;
}
6)
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) ;
}
7)
What does the following program print ?
#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 ) ;
}
8)
What does the following print ?
#include <iostream>
using namespace std;
int main()
{
int x1 = 2 ;
int y1 = 5 ;
x1 += x1 + y1 * 2 ;
// x1 = x1 + x1 + y1 * 2 ;
x1 = x1 / 2 ;
x1 = x1 + 0.1 ;
y1 = y1 % 2 ;
cout << "x1:" << x1 << " y1:" << y1 << endl ;
return 0;
}
9)
#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.
10)
#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 ) ;
}
1)
What does the below print ?
#include <iostream>
using namespace std ;
int main()
{
int var1 = 20 ;
int var2 = 30 ;
int* ptr1 ;
int* ptr2 ;
int* temp ;
ptr1 = &var1 ;
ptr2 = &var2 ;
cout << *ptr1 << endl ;
cout << var2 << endl ;
cout << *ptr2 << endl ;
temp = ptr1 ; ptr1 = ptr2 ;
ptr2 = temp ;
cout << *ptr1 << endl ;
cout << var1 << endl ;
cout << *ptr2 << endl ;
}
2)
What does the below print ?
#include <iostream>
using namespace std ;
int main()
{
int arr1[2][3] = { {1,2,3} , { 4,5,6} } ;
for( int i1=0 ; i1<3 ; i1++ )
{
arr1[0][i1] = arr1[0][i1] + 1 ;
arr1[1][i1] = arr1[1][i1] - 1 ;
}
for( int i1=0 ; i1<2 ; i1++ )
{
for( int i2=0 ; i2<3 ; i2 = i2 +2 )
{
cout << arr1[i1][i2] << " " ;
} //for
cout << endl ;
} //for
}
3)
What does the following print ?
#include <iostream>
using namespace std ;
void showLocal()
{
static int localNum = 5 ;
cout << "localNum is " << localNum << endl;
localNum++ ;
}
void showLocalOne()
{
int localNum = 5 ;
cout << "localNum in showLocalOne is " << localNum << endl;
localNum++ ;
}
int main()
{
showLocal();
showLocal();
showLocalOne();
showLocalOne();
return 0;
}
4)
The following program uses linear search to search a particular section of an array. What will it print ?
#include <iostream>
using namespace std ;
int searchList( const int list[], int size, int value, int leftIndex, int rightIndex )
{
int index = 0; // Used as a subscript to search array
int position = -1; // To record position of search value
bool found = false; // Flag to indicate if the value was found
for( int i1=leftIndex ; i1<= rightIndex ; i1++ )
{
cout << list[i1] << " : " ;
if (list[i1] == value) // If the value is found
{
cout << list[i1] << " : " ;
return ( i1 ) ;
}
// Go to the next element
}
return position; // Return the position, or -1
}
int main()
{
int arr1[] = { 5 , 2 , 1 , 9 , 3 , 4 } ;
searchList ( arr1 , 6, 3 , 2, 4 ) ;
cout << endl ;
searchList ( arr1 , 6, 3 , 1, 3 ) ;
cout << endl ;
searchList ( arr1 , 6, 10 , 1, 3 ) ;
cout << endl ;
return ( 0 ) ;
}
5)
What does the following print ?
#include <iostream>
using namespace std ;
int binarySearch(const int array[], int numElems, int value)
{
int first = 0, // First array element
last = numElems - 1, // Last array element
middle, // Midpoint of search
position = -1; // Position of search value
bool found = false; // Flag
while (!found && first <= last)
{
middle = (first + last) / 2; // Calculate midpoint
cout << array[middle] << " : " ;
if (array[middle] == value) // If value is found at mid
{
found = true;
position = middle;
}
else if (array[middle] > value) // If value is in lower half
last = middle - 1;
else
first = middle + 1; // If value is in upper half
}
cout << position << " : " ;
return position;
}
int main()
{
int arr1[] = { 1 , 2 , 3 , 4 , 6 , 9 } ;
binarySearch( arr1 , 6, 3 ) ;
cout << endl ;
binarySearch( arr1 , 6, 5 ) ;
cout << endl ;
binarySearch( arr1 , 6, 1 ) ;
cout << endl ;
return ( 0 ) ;
}
6)
What does the following program print ?
#include <iostream>
using namespace std ;
void printArray( int array1[], int size )
{
for( int i1=0 ; i1<size ; i1++ )
{
cout << array1[i1] << " " ;
}
cout << endl ;
}
void selectionSort( int array[], int size )
{
int startScan, minIndex, minValue;
for (startScan = 0; startScan < (size - 1); startScan++)
{
minIndex = startScan;
minValue = array[startScan];
for(int index = startScan + 1; index < size; index++)
{
if (array[index] < minValue)
{
minValue = array[index];
minIndex = index;
}
}
array[minIndex] = array[startScan];
array[startScan] = minValue;
printArray( array , size ) ;
}
}
int main()
{
int arr1[] = { 5 , 8 , 2 , 7 } ;
selectionSort( arr1 , 4 ) ;
cout << endl ;
return ( 0 ) ;
}
7)
#include <iostream>
#include <cstring>
using namespace std ;
int main()
{
char ch1 = 'a' ;
char buffer[256] ;
cout << char( toupper( ch1 ) ) << endl ;
cout << char( toupper( ch1 + 1 ) ) << endl ;
cout << char( tolower( ch1 + 2 ) ) << endl ;
strcpy ( buffer, "Henry " ) ;
strcat ( buffer , "Armstrong" ) ;
cout << strlen( buffer ) << " : " <<
buffer << endl ;
return ( 0 ) ;
}
8)
What does the following print ?
#include <iostream>
#include <cstring>
using namespace std ;
int main()
{
int arr1[] = { 10 , 20, 30, 40 } ;
int* ptr1 ;
int index = 0 ;
ptr1 = arr1 ;
index++ ;
cout << *( ptr1 + ++index ) << endl ;
cout << *( arr1 + ++index ) << endl ;
cout << arr1[--index] << endl ;
cout << ptr1[--index] << endl ;
return ( 0 ) ;
}
9)
What does the following print ?
#include <string>
#include <iostream>
using namespace std;
struct EmployeePay
{
string name; // Employee name
int empNum; // Employee number
double payRate; // Hourly pay rate
double hours; // Hours worked
double grossPay; // Gross pay
};
void print( EmployeePay emp )
{
cout << emp.name << " : " << emp.empNum << " : " <<
emp.payRate << " : " << emp.hours << " : " << emp.grossPay <<
endl ;
}
int main()
{
EmployeePay employee1 = {"Betty Ross", 1, 18.75 } ;
EmployeePay employee2 = {"Jill Sandburg", 2, 40, 10 , 400 } ;
print( employee1 ) ;
print( employee2 ) ;
}
10)
What will the following print ?
#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 ;
}
~Demo()
{
cout << "Inside the destructor." << 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 ) ;
}