Question 1: What is the output of the following snippet?
#include <iostream>
using namespace std;
int main()
{
int *it[3];
for(int i = 0; i < 3; i++) {
it[i] = new int [i + 1];
for(int j = 0; j < i + 1; j++)
it[i][j] = 10 * i + j;
}
cout << it[2][2];
for(int i = 0; i < 3; i++)
delete [] it[i];
}
- 11
- Compilation fails
- 33
- 22
Question 2: What is the output of the following snippet?
#include <iostream>
using namespace std;
int main()
{
short s = 1;
int i = 2;
float f = 4.4;
double d = 6.6;
cout << i/static_cast<float>(s) + int(f)/i + (long)d/s;
return 0;
}
- 8.0
- Compilation fails
- 10
- 8.8
Question 3: What is the output of the following snippet?
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<float *> ft = { new float[3], new float[3], new float[3] };
float *p = nullptr;
for(int i = 0; i < 3; i++) {
p = ft[i];
*p = p[1] = *(p + 2) = 10 * i;
}
cout << ft[1][1];
delete [] ft[0];
delete [] ft[1];
delete [] ft[2];
}
- 10
- 20
- Compilation fails
- 30
Question 4: What is the output of the following snippet?
#include <iostream>
using namespace std;
namespace S {
int A = 1;
}
namespace S {
int B = A + 2 ;
}
int main()
{
S::A = S::A + 1;
{
using namespace S;
++B;
}
cout << S::B << S::A;
return 0;
}
- 22
- 32
- 42
- Compilation fails
Question 5: What is the output of the following snippet?
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s = “ABC”;
for(int i = 1; i < s.length(); i += 2)
s[i – 1] = s[i] + ‘a’ – ‘A’;
cout << s;
}
- bBc
- bBC
- Compilation fails
- aAcC
Question 6: What is the output of the following snippet?
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1 = “aleph”;
string s2 = “alpha”;
string s;
s1.swap(s2);
s2.swap(s);
s.swap(s2);
cout << s2;
}
- alpha
- Compilation fails
- aleph
- It prints an empty string
Question 7: What is the output of the following snippet?
#include <iostream>
using namespace std;
int main()
{
string s1 = “Ab”;
string s2 = “Abc”;
cout << s1.compare(s1);
}
- Compilation fails
- 1
- -1
- 0
Question 8: What is the output of the following snippet?
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s = “AB”;
s.append(s).push_back(s[s.length() – 1]);
cout << s;
}
- ABABA
- ABAB
- ABABB
- Compilation fails
Question 9: What is the output of the following snippet?
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s = “a”;
cout << s << “b” + “c”;
}
- a
- ab
- Compilation fails
- abc
Question 10: What is the output of the following snippet?
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s = “0123”;
cout << s.substr(1,3).substr(2).substr();
}
- It prints an empty string
- 3
- 123
- Compilation fails
Question 11: What is the output of the following snippet?
#include <iostream>
#include <string>
using namespace std;
int main()
{
int i = 2;
string s = “2”;
cout << s + i;
}
- 2
- 2
- 4
- Compilation fails
Question 12: What is the output of the following snippet?
#include <iostream>
#include <string>
using namespace std;
int main()
{
int i = 2;
string s = “2”;
cout << s + i;
}
- 2
- 2
- 4
- Compilation fails
Question 13: What is the output of the following snippet?
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector<string> t = { “alpha”, “beta”, “gamma” };
int *cnt = new int[3], *p = &cnt[0];
for(int i = 0; i < t.size(); i++)
*p++ = t[i].length();
cout << cnt[0] << cnt[1] << cnt[2];
delete [] cnt;
}
- 012
- 545
- 111
- Compilation fail
Question 14: What is the output of the following snippet?
#include <iostream>
using namespace std;
int main()
{
short s = 1;
int i = 2;
float f = 4.4;
double d = 8.8;
cout << s/i + f/i + d/s;
}
- 11
- Compilation fails
- 6.6
- 4.4
Question 15: What is the output of the following snippet?
#include <iostream>
using namespace std;
namespace S1 {
int A = 1;
}
namespace S2 {
int A = 2 ;
}
int main()
{
{ using namespace S1;
S2::A = A + 1;
}
{ using namespace S2;
S1::A = A + 1;
}
cout << S1::A << S2::A;
}
- Compilation fails
- 32
- 33
- 23
Question 16: What is the output of the following snippet?
include <iostream>
using namespace std;
int main()
{
int i = 2;
float f = 5.8;
f = static_cast<int>(f);
i = static_cast<float>(i);
cout << f/i;
}
- 1
- Compilation fails
- 2.5
- 3
Question 17: What is the output of the following snippet?
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s = “a”;
cout << s + “b” + “c”;
}
- abc
- ab
- Compilation fails
- a
Question 18: What is the output of the following snippet?
#include <iostream>
#include <string>
using namespace std;
int main() {
string s1 = “ab”;
string s2 = “Abc”;
if(s1 > s2)
cout << “yes”;
else
cout << “NO”;
}
- Compilation fails
- yes
- It prints nothing
- NO
Question 19: What is the output of the following snippet?
#include <iostream>
namespace SpaceA {
int A;
}
namespace SpaceB {
int A;
}
using namespace SpaceA, SpaceB;
int main()
{
SpaceA::A = SpaceB::A = 1;
std::cout << A + 1;
}
- Compilation fails
- 1
- 0
- 2
Question 20: What is the output of the following snippet?
#include <iostream>
using namespace std;
int main() {
string s1 = “1”;
string s2 = “12”;
cout << s1.compare(s2);
}
- –1
- 0
- 1
- Compilation fails
Question 21: What is the output of the following snippet?
#include <iostream>
using namespace std;
int main()
{
int i = 2;
float f = 4.4;
cout << f % static_cast<float>(i);
}
- 0
- 2
- 0.2
- Compilation fails