Programming in C++: Assignment Week 1
Programming in C++: Assignment Week 1
Total Marks : 20
September 3, 2018
Question 1
Which of the following statement is NOT true for a variable? MCQ, Marks 1
Answer: c)
Explanation: A variable name cannot begin with a number. Refer to Slides (Module-1)
1
Question 2
What is the output of following code snippet: MCQ, Marks 1
a) 2.0
b) 3.0
c) 4.0
d) 5.0
Answer: b) 3.0
Explanation: p points to the base address of array i , so (p+2) is a pointer to base address
plus two locations’ and *(p+2) means value at that location which is i[2] i.e. 3.0 in this case.
2
Question 3
Consider the following statements:
int *p;
int i, k;
i = 142;
k = i;
p = &i;
Which of the following statement changes the value of i to 145 ? MCQ, Marks 1
a) k = 145;
b) *k = 145;
c) p = 145;
d) *p = 145;
Answer: d) *p = 145;
Explanation: statement ’p = &i;’ makes ’p’ pointer to i and statement ’*p = 145’ assigns
the value 145 to i by using ponter variable ’p’.
3
Question 4
What will be the output of the following program: MCQ, Marks 1
#include <stdio.h>
int sum(int x, int y) {
return (++x + ++y);
}
int main () {
int i = sum(3, 5);
printf("%d",i);
return 0;
}
a) 8
b) 9
c) 10
Answer: c) 10
Explanation: prefix operators are used with variables. Therefore, first increment will be
done then value will be returned. (++x + ++y) (++3 + ++5) (4 + 6) (10)
4
Question 5
Consider the following code segment and answer the question-5 and 6 (sizeof(int) = 4,
sizeof(float) = 8, sizeof(unsigned char) = 1):
int main() {
union Data {
int i_;
float f_;
unsigned char str[20];
} data;
printf("size = %d\n", sizeof(data)); // ....#S1
data.i_ = 10;
data.f_ = 220.5;
printf( "data.i_ : %d\n", data.i_); // ....#S2
return 0;
}
Q. 5
What value will be printed by the statement marked as #S1? MCQ, Marks 2
a) 28
b) 32
c) 20
d) 24
Answer: d) 24
Explanation: As we know union allocates memory only for the largest data member. The
answer could have 20 by considering the variable ’str’, (All the compiler have fixed size for
unsigned char which is 1 Byte), which is the highest one. But Size of the float is given as ’8’,
so the size of the union should be the next nearest multiple of 8 which is greater than or equal
to 20. It happens due to the alignment of memory. If size of float is 4 bytes, then the answer
is 20 bytes. In most of the machines size of float is 4 bytes, that’s why everyone may get 20
bytes as answer. If you change the data type to double (i.e 8 bytes)in the QS you will get 24
bytes ()
Question 6
What value will be printed by the statement marked as #S2 in Question-5? MCQ, Marks 2
a) 10
b) 220.5
c) 230.5
d) Unpredictable Value
Answer: d) Unpredictable Value
Explanation: Since union uses the same memory location for each of the data member, the
last assigned value 220.5 occupies the entire 20 byte memory. But when we are trying to access
’i’, a part of the entire memory will be accessed and we don’t know / sure about the content
of that.
5
Question 7
What will be printed upon the execution of the following program? MCQ, Marks 2
#include <stdio.h>
#include <stdlib.h>
int main() {
int count = 10, sum = 0, i;
int *arr = (int*) malloc(sizeof(int)*count);
for(i = 0; i < count; i++) {
arr[i] = i;
sum += arr[i];
}
printf("Array Sum:%d ", sum);
return 0;
}
a) 45
b) 55
c) Array Sum: 45
d) Compilation Error
6
Question 8
What will be the output of the following program? MCQ, Marks 2
#include <iostream>
using namespace std;
int main() {
int x1 = 5, x2 = 10, x3 = 15, x4 = 20, x5 = 25;
int *arr[] = {&x1, &x2, &x3, &x4, &x5};
cout<<*arr[*arr[3]-*arr[1]-10];
return 0;
}
a) 5
b) 10
c) 15
d) 20
e) Compilation Error
Answer: a) 5
Explanation:
Step-1: *arr[*arr[3]-*arr[1]-10];
step-2: *arr[20-10-10]
Step-3: *arr[0] which is the value of x1 i.e. 5
7
Question 9
What will be the output of the following program? MCQ, Marks 2
#include <iostream>
#include <algorithm>
using namespace std;
int main () {
int data[] = {66, 35, 45, 39, 21};
sort (data, data+5);
for (int i = 0; i < 5; i++)
cout << data[i] << " ";
return 0;
}
a) 21 39 45 65 66
b) 21 35 39 45 66
c) 66 45 39 35 21
d) 21 35 45 66 69
Answer: b) 21 35 39 45 66
Explanation: Sorting of array elements in ascending order using the default sort function of
algorithm library.
8
Question 10
What will be the output of the following program? MCQ, Marks 2
#include<iostream>
#include<string.h>
#include<stack>
using namespace std;
int main() {
char str[]= "ABCDE";
stack<char> s;
for(int i = 0; i < strlen(str); i++)
s.push(str[i]);
for(int i = 0; i < strlen(str) - 1; i++) {
s.pop();
cout<<s.top();
}
return 0;
}
a) EDCBA
b) EDCB
c) DCBA
d) ABCDE
Answer: c) DCBA
Explanation: Here stack s is of character kind. s.push(), push 5 characters (= length of
the ’str’) one by one into the stack. s.pop(), popping 4 characters one by one from stack
and displays it using LIFO principle.
9
Question 11
Consider a program for post-fix evaluation. Fill in the blanks marked by A# and B# to
generate the given output? MCQ, Marks 2
#include<iostream>
#include <stack>
using namespace std;
int main() {
char postfix[] = {’5’,’8’,’3’,’4’,’2’,’/’,’*’,’-’,’+’}, ch;
stack<int> s;
for(int i = 0; i < 9; i++) {
ch = postfix[i];
if (isdigit(ch))
s.push(ch-’0’);
else {
int op1 = _________________ // A#
int op2 = _________________ // B#
switch(ch) {
case ’/’: s.push(op2 / op1); break;
case ’*’: s.push(op2 * op1); break;
case ’+’: s.push(op2 + op1); break;
case ’-’: s.push(op2 - op1); break;
}
}
}
cout<< "\nEvaluation of Postfix Expression is: " << s. top();
return 0;
}
Output: Evaluation of Postfix Expression is: 7
10
Question 12
Consider the definitions of the following member functions of vector:
push back(): function is used to push elements into a vector from the back.
pop back(): function is used to pop or remove elements from a vector from the back.
back(): function can be used to fetch the last element of a vector.
empty(): function is used to check if the vector is empty or not
#include <iostream>
#include <vector>
using namespace std;
int main () {
vector<int> myvector;
int sum (0);
myvector.push_back(100);
myvector.push_back(200);
myvector.push_back(300);
while (!myvector.empty()) {
sum += myvector.back();
myvector.pop_back();
}
cout << sum << ", " << myvector.size();
return 0;
}
a) 600, 3
b) 600, 0
c) 0, 3
d) 0, 0
Answer: b)
Explanation: Firstly, all the elements are pushed into the vector ’myvector’. Then they
are removed (popped out) one by one and added to each other. Now the vector ’myvector’
is empty so its size is 0.
11