Home »
Aptitude Questions and Answers »
C++ Aptitude Questions and Answers
C++ const Keyword Aptitude Questions and Answers
C++ const Keyword Aptitude: This section contains C++ const Keyword Aptitude Questions and Answers with explanations.
Submitted by Nidhi, on February 18, 2021
1) There are the following statements that are given below, which of them are correct about const keyword in C++?
- The const keyword is used to create constants. That's value cannot be changed during program execution.
- The constant created in C++ must be initialized at the time of declaration.
- The member initializer list is used to initialize const members of the class.
- All the above.
Options:
- A and B
- A and C
- B and C
- D
Correct Answer - 4
D
Explanation:
All given statements are correct about the const keyword in C++.
2) In C++, the const keyword can be used with?
- Variables
- Member functions
- Objects
- Pointers
Options:
- A and B
- A and C
- B, C, and D
- A, B, C, and D
Correct Answer - 4
A, B, C, and D
Explanation:
All given options can be used with the const keyword.
3) What is the correct output of given code snippets?
#include <iostream>
using namespace std;
int main()
{
const int A;
A = 10;
cout << A << endl;
return 0;
}
Options:
- 10
- Compile-time error
- Runtime error
- Linker error
Correct Answer - 2
Compile-time error
Explanation:
The above program will generate the compile-time error because we need to initialize constants at the time of declaration.
4) What is the correct output of given code snippets?
#include <iostream>
using namespace std;
const int fun()
{
return 10;
}
int main()
{
const int val = fun();
cout << val;
return 0;
}
Options:
- 10
- Compile-time error
- Runtime error
- Linker error
Correct Answer - 1
10
Explanation:
The above code will print "10" on the console screen.
5) What is the correct output of given code snippets?
#include <iostream>
using namespace std;
int main()
{
const int A;
int* p = &A;
*p = 20;
cout << A << endl;
return 0;
}
Options:
- 10
- Compile-time error
- Vary to the compiler to compiler
- Linker error
Correct Answer - 3
Vary to the compiler to compiler
Explanation:
Above will perfectly execute on some compilers but it may generate a compile-time error on most of the C++ compilers.
6) What is the correct output of given code snippets?
#include <iostream>
using namespace std;
int main()
{
int A = 10;
int* const p = &A;
*p = 20;
cout << A << endl;
return 0;
}
Options:
- 10
- 20
- Compile-time error
- Vary to the compiler to compiler
Correct Answer - 2
20
Explanation:
The above code will print "20" on the console screen.
7) What is the correct output of given code snippets?
#include <iostream>
using namespace std;
int main()
{
int A=10;
int const *p = &A;
*p = 20;
cout<<A<<endl;
return 0;
}
Options:
- 10
- 20
- Compile-time error
- Vary to the compiler to compiler
Correct Answer - 3
Compile-time error
Explanation:
The above code will generate the compile-time error, because here *p is constant that we cannot modify the value of the *p.
8) What is the correct output of given code snippets?
#include <iostream>
using namespace std;
int main()
{
int A=10;
const int *p = &A;
*p = 20;
cout<<A<<endl;
return 0;
}
Options:
- 10
- 20
- Compile-time error
- Vary to the compiler to compiler
Correct Answer - 3
Compile-time error
Explanation:
The above code will generate the compile-time error, because here *p is constant that we cannot modify the value of the *p.
9) What is the correct output of given code snippets?
#include <iostream>
using namespace std;
class Sample
{
const int X;
const int Y;
public:
Sample()
{
X=10;
Y=20;
}
void print()
{
cout<<X<<" "<<Y<<endl;
}
};
int main()
{
Sample S;
S.print();
return 0;
}
Options:
- 10 20
- Compile-time error
- Vary to the compiler to compiler
- Runtime error
Correct Answer - 2
Compile-time error
Explanation:
The above code will generate the compile-time error because we cannot assign values into const data members.
10) What is the correct output of given code snippets?
#include <iostream>
using namespace std;
class Sample
{
const int X;
const int Y;
public:
Sample(): X(10),Y(20)
{}
void print()
{
cout<<X<<" "<<Y<<endl;
}
};
int main()
{
Sample S;
S.print();
return 0;
}
Options:
- 10 20
- Compile-time error
- Vary to the compiler to compiler
- Runtime error
Correct Answer - 1
10 20
Explanation:
The above code will print "10 20" on the console screen.
11) What is the correct output of given code snippets?
#include <iostream>
using namespace std;
class Sample
{
int X;
int Y;
public:
Sample()
{
X=10;
Y=20;
}
const void setVal()
{
X=15;
Y=25;
}
void print()
{
cout<<X<<" "<<Y<<endl;
}
};
int main()
{
Sample S;
S.setVal();
S.print();
return 0;
}
Options:
- 10 20
- 15 25
- Vary to the compiler to compiler
- Compile-time error
Correct Answer - 2
15 25
Explanation:
The above code will print "15 25" on the console screen.
12) What is the correct output of given code snippets?
#include <iostream>
using namespace std;
class Sample
{
int X;
int Y;
public:
Sample()
{
X=10;
Y=20;
}
void setVal() const
{
X=15;
Y=25;
}
void print()
{
cout<<X<<" "<<Y<<endl;
}
};
int main()
{
Sample S;
S.setVal();
S.print();
return 0;
}
Options:
- 10 20
- 15 25
- Vary to the compiler to compiler
- Compile-time error
Correct Answer - 4
Compile-time error
Explanation:
The above code will generate the compile-time error because we cannot modify the value in the constant member function.
13) What is the correct output of given code snippets?
#include <iostream>
using namespace std;
class Sample
{
int X;
int Y;
public:
Sample()
{
X=10;
Y=20;
}
void setVal()
{
X=15;
Y=25;
}
void print()
{
cout<<X<<" "<<Y<<endl;
}
};
int main()
{
const Sample S;
S.setVal();
S.print();
return 0;
}
Options:
- 10 20
- 15 25
- Vary to the compiler to compiler
- Compile-time error
Correct Answer - 4
Compile-time error
Explanation:
The above code will generate the compile-time error because we cannot modify data members if the object is defined as a constant.
14) What is the correct output of given code snippets?
#include <iostream>
using namespace std;
const int fun(int a, int b)
{
return a+b;
}
int main()
{
const int val = fun(10,20);
cout<<val;
return 0;
}
Options:
- 30
- Runtime error
- Vary to the compiler to compiler
- Compile-time error
Correct Answer - 1
30
Explanation:
The above code will print "30" on the console screen.
15) What is the correct output of given code snippets?
#include <iostream>
using namespace std;
void fun(const int a, int b)
{
a=10;
b=20;
}
int main()
{
int A = 10;
int B = 10;
fun(A,B);
cout<<A<<" "<<B;
return 0;
}
Options:
- 10 20
- Runtime error
- Vary to the compiler to compiler
- Compile-time error
Correct Answer - 4
Vary to the compiler to compiler
Explanation:
The above code will generate the compile-time error because we cannot modify the const arguments of the function.