Test Series - cpp

Test Number 28/102

Q: What is the use of Namespace?
A. To encapsulate the data
B. To structure a program into logical units
C. Encapsulate the data & structure a program into logical units
D. It is used to mark the beginning of the program
Solution: The main aim of the namespace is to understand the logical units of the program and to make the program so robust.
Q: What is the general syntax for accessing the namespace variable?
A. namespace::operator
B. namespace,operator
C. namespace#operator
D. namespace$operator
Solution: To access variables from namespace we use following syntax.
namespace :: variable;
General syntax:
namespace X{ int a;}
cout<
Q: Which keyword is used to access the variable in the namespace?
A. using
B. dynamic
C. const
D. static
Solution: using keyword is used to specify the name of the namespace to which the variable belongs.
eg.
namespace A{ int a = 5;}
namespace B{ int a = 10;}
using namespace A;
cout<
Q: Pick the incorrect statement for namespaces in C++.
A. Namespace declarations are always global scope
B. Keyword namespace is used at the starting of a namespace definition
C. Namespace has access specifiers like private or public
D. Namespace definitions can be nested
Solution: Namespace does not have any specifiers associated with it like classes or structures.
Q: Which operator is used for accessing a member of namespace?
A. :
B. ::
C. ->
D. .
Solution: Scope resolution operator(::) is used for accessing a member of a namespace. example:
namespace A{
	int var;
}
A::var = 5;
Q: Which is the correct syntax of declaring a namespace?
A. namespace A{ int i }
B. namespace B{ int i; };
C. namespace C{ int i; }
D. Namespace D{ int i }
Solution: A namespace definition always starts with the namespace keyword so definition with Namespace(capital N) is wrong. namespace does is not terminated by a semicolon hence the definition with a semicolon is wrong. every variable declaration in C++ should end with semicolon therefore namespace containing ‘int i’ without semicolon is wrong.
Q: What is the correct syntax of defining a namespace?
A. namespace name{}
B. Namespace name{};
C. namespace name{};
D. typedef namespace name{} NAME
Solution: A namespace:
-Starts with keyword namespace
-Followed by identifier
-All members inside the braces{}
-No semicolon at the end
namespace identifier{}.
Q: To where does the program control transfers when the exception is arisen?
A. catch
B. handlers
C. throw
D. try
Solution: When an exception is arisen mean, the exception is caught by handlers and then it decides the type of exception.
Q: Which keyword is used to check exception in the block of code?
A. catch
B. throw
C. try
D. handlers
Solution: The try() statement is used for exceptions in c++.
Q: What will happen when the exception is not caught in the program?
A. error
B. program will execute
C. block of that code will not execute
D. program will execute & displays wrong output
Solution: When exceptions are not caught in any program then program throws error.

You Have Score    /10