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++?

  1. The const keyword is used to create constants. That's value cannot be changed during program execution.
  2. The constant created in C++ must be initialized at the time of declaration.
  3. The member initializer list is used to initialize const members of the class.
  4. All the above.

Options:

  1. A and B
  2. A and C
  3. B and C
  4. D

2) In C++, the const keyword can be used with?

  1. Variables
  2. Member functions
  3. Objects
  4. Pointers

Options:

  1. A and B
  2. A and C
  3. B, C, and D
  4. A, B, C, and D

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:

  1. 10
  2. Compile-time error
  3. Runtime error
  4. Linker error

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:

  1. 10
  2. Compile-time error
  3. Runtime error
  4. Linker error

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:

  1. 10
  2. Compile-time error
  3. Vary to the compiler to compiler
  4. Linker error

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:

  1. 10
  2. 20
  3. Compile-time error
  4. Vary to the compiler to compiler

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:

  1. 10
  2. 20
  3. Compile-time error
  4. Vary to the compiler to compiler

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:

  1. 10
  2. 20
  3. Compile-time error
  4. Vary to the compiler to compiler

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:

  1. 10 20
  2. Compile-time error
  3. Vary to the compiler to compiler
  4. Runtime error

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:

  1. 10 20
  2. Compile-time error
  3. Vary to the compiler to compiler
  4. Runtime error

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:

  1. 10 20
  2. 15 25
  3. Vary to the compiler to compiler
  4. Compile-time error

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:

  1. 10 20
  2. 15 25
  3. Vary to the compiler to compiler
  4. Compile-time error

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:

  1. 10 20
  2. 15 25
  3. Vary to the compiler to compiler
  4. Compile-time error

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:

  1. 30
  2. Runtime error
  3. Vary to the compiler to compiler
  4. Compile-time error

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:

  1. 10 20
  2. Runtime error
  3. Vary to the compiler to compiler
  4. Compile-time error





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.