Solved MCQs on C++


TYPES

1. What is the size of wchar_t in C++?
a) 2
b) 4
c) 2 or 4
d) based on the number of bits in the system
View Answer
Answer:d
Explanation: Compiler wants to make CPU as more efficient in accessing the next value.
2. Pick the odd one out
a) array type
b) character type
c) boolean type
d) integer type
View Answer
Answer:a
Explanation: Array type is not the basic type and it is constructed using the basic type.
3. Which datatype is used to represent the absence of parameters?
a) int
b) short
c) void
d) float
View Answer
Answer:c
Explanation: void will not return anything.
4. What does a escape code represent?
a) alert
b) backslash
c) tab
d) form feed
View Answer
Answer:a
Explanation: Because a is used to produce a beep sound.
5. Which type is best suited to represent the logical values?
a) integer
b) boolean
c) character
d) all of the mentioned
View Answer
Answer:b
Explanation: Logical values can be either true or false, so the boolean type is suited for it.
6. Identify the user-defined types from the following?
a) enumeration
b) classes
c) both a and b
d) int
View Answer
Answer:c
Explanation:They must be defined by the users before use unlike the other types which are readily available.
7. Which of the following statements are true?
    int f(float)
a) f is a function taking an argument of type int and retruning a floating point number
b) f is a function taking an argument of type float and returning a integer.
c) f is a function of type float
d) none of the mentioned
View Answer
Answer:b
Explanation: The argument that is passed to a function f is of float type and the function finally retruns a value that id is of integer type.
8. The value 132.54 can represented using which data type?
a) double
b) void
c) int
d) bool
View Answer
Answer:a
Explanation: The given value is with decimal points, so float or double can be used.
9. When a language has the capability to produce new data type mean, it can be called as
a) overloaded
b) extensible
c) encapsulated
d) reprehensible
View Answer
Answer:b
Explanation: Extensible is used to add new features to C++.
10. Pick the odd one out.
a) integer, character, boolean, floating
b) enumeration, classes
c) integer, enum, void
d) arrays, pointer, classes
View Answer
Answer:c
Explanation: Option a consists of all fundamental types, option b consists of user-definied types and option d consists of derived types but option c is a mixture
BOOLEANS
1. Is bool a fundamental datatype in C++?
a) Yes
b) No, it is a typedef of unsigned char
c) No, it is an enum of {false,true}
d) No, it is expanded from macros
View Answer
Answer:a
Explanation: C++ has bool as a fundamental data type.
2. Find the odd one out:
a) std::vector<int>
b) std::vector<short>
c) std::vector<long>
d) std::vector<bool>
View Answer
Answer:d
Explanation: std::vector<bool> is a specialized version of vector, which is used for elements of type bool and optimizes for space. It behaves like the unspecialized version of vector and the storage is not necessarily an array of bool values, but the library implementation may optimize storage so that each value is stored in a single bit.
3. What is the value of the bool?
  1. bool is_int(789.54)
a) True
b) False
c) 1
d) none of the mentioned
View Answer
Answer:b
Explanation: The given number is a double not an integer, so the function returns 0 which is boolean false.
4. What happens when a null pointer is converted into bool?
a) An error is flagged
b) bool value evaluates to true
c) bool value evaluates to false
d) the statement is ignored
View Answer
Answer:c
Explanation: A pointer can be implicitly converted to a bool. A nonzero pointer converts to true and zerovalued pointer converts to false.
5. Which of the following statements are false?
a) bool can have two values and can be used to express logical expressions.
b) bool cannot be used as the type of the result of the function.
c) bool can be converted into integers implicitly
d) a bool value can be used in arithemetic expressions.
View Answer
Answer:b
Explanation: None.
6. For what values of the expression is an if-statement block not executed?
a) 0 and all negative values
b) 0 and -1
c) 0
d) 0, all negative values, all positive values except 1
View Answer
Answer:c
Explanation: The if-statement block is only not executed when the expression evaluates to 0. It’s just syntactic sugar for a branch-if-zero instruction.
7. Which of the two operators ++ and — work for the bool datatype in C++?
a) None
b) ++
c) —
d) Both
View Answer
Answer:b
Explanation: Due to history of using integer values as booleans, if an integer is used as a boolean, then incrementing will mean that whatever its truth value before the operation, it will have a truth-value of true after it. However, it’s not possible to predict the result of — given knowledge only of the truth value of x, as it could result in false.
8. What is the output of the following program?
  1.     #include <iostream>
  2.     using namespace std;
  3.     int f(int p, int q)
  4.     {
  5.         if (p > q)
  6.             return p;
  7.         else
  8.             return q;
  9.     }
  10.     main()
  11.     {
  12.         int a = 5, b = 10;
  13.         int k;
  14.         bool x = true;
  15.         bool y = f(a, b);
  16.         k =((a * b) + (x + y));
  17.         cout << k;
  18.     }
a) 55
b) 62
c) 52
d) none of the mentioned
View Answer
Answer:c
Explanation: None.
9. What is the value of p?
  1.     #include <iostream>
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         int p;
  6.         bool a = true;
  7.         bool b = false;
  8.         int x = 10;
  9.         int y = 5;
  10.         p = ((x | y) + (a + b));
  11.         cout << p;
  12.         return 0;
  13.     }
a) 0
b) 16
c) 12
d) 2
View Answer
Answer:b
Explanation: None.
10. Evaluate the following
(false && true) || false || true
a) 0
b) 1
c) false
d) none of the mentioned
View Answer
Answer:b
Explanation: None.
.CHARACTER TYPE
1. How many characters are specified in the ASCII scheme?
a) 64
b) 128
c) 256
d) none of the mentioned
View Answer
Answer:b
Explanation: None.
2. Select the right option.
Given the variables p, q are of char type and r, s, t are of int type
1. t = (r * s) / (r + s);
2. t = (p * q) / (r + s);
a) 1 is true but 2 is false
b) 1 is false and 2 is true
c) both 1 and 2 are true
d) both 1 and 2 are false
View Answer
Answer:c
Explanation: Every character constant has an integer value. Also char belongs to the integral type hence arithmetic and logical operations can be performed on them.
3. Which of the following belongs to the set of character types?
a) char
b) wchar_t
c) only a
d) both a and b
View Answer
Answer:d
Explanation: wchar_t and char is used to represent wide character and character.
4. What will be the output of this program?
  1.     #include <iostream>
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         char c = 74;
  6.         cout << c;
  7.         return 0;
  8.     }
a) A
b) N
c) J
d) I
View Answer
Answer:c
Explanation: The literal value for 74 is J. So it will be printing J.
5. How do we represent a wide character of the form wchar_t?
a) L’a’
b) l’a’
c) L[a] d) la
View Answer
Answer:a
Explanation: A wide character is always indicated by immediately preceding the character literal by an L.
6. What is the output of this program?
  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         char a = '\012';
  5.  
  6.         printf("%d", a);
  7.         return 0;
  8.     }
a) Compiler error
b) 12
c) 10
d) Empty
View Answer
Answer:c
Explanation: The value ‘\012’ means the character with value 12 in octal, which is decimal 10.
7. In C++, what is the sign of character data type by default?
a) Signed
b) Unsigned
c) Implementation dependent
d) None of these
View Answer
Answer:c
Explanation: The standard does not specify if plain char is signed or unsigned. There are three distinct character types according to the standard: char, signed char and unsigned char.
8. Is the size of character literals different in C and C++?
a) Implementation defined
b) Can’t say
c) Yes, they are different
d) No, they are not different
View Answer
Answer:c
Explanation: In C++, sizeof(‘a’) == sizeof(char) == 1. In C however, sizeof(‘a’) == sizeof(int).
9. Suppose in a hypothetical machine, the size of char is 32 bits. What would sizeof(char) return?
a) 4
b) 1
c) Implementation dependent
d) Machine dependent
View Answer
Answer:b
Explanation: The standard does NOT require a char to be 8-bits, but does require that sizeof(char) return 1.

INTEGER TYPE

1. The size_t integer type in C++ is?
a) Unsigned integer of at least 64 bits
b) Signed integer of at least 16 bits
c) Unsigned integer of at least 16 bits
d) Signed integer of at least 64 bits
View Answer
Answer:c
Explanation: The size_t type is used to represent the size of an object. Hence, it’s always unsigned. According to the language specification, it is at least 16 bits.
2. What is the output of the following program?
  1.     #include <iostream>
  2.     using namespace std;
  3.     int main()
  4.     {
  5.      int x = -1;
  6.         unsigned int y = 2;
  7.  
  8.         if(x > y) {
  9.          cout << "x is greater";
  10.      } else {
  11.       cout << "y is greater";
  12.      }
  13.     }
a) x is greater
b) y is greater
c) Implementation defined
d) Arbitrary
View Answer
Answer:a
Explanation: x is promoted to unsigned int on comparison. On conversion x has all bits set, making it the bigger one.
3. Which of these expressions will return true if the input integer v is a power of two?
a) (v | (v + 1)) == 0;
b) (~v & (v – 1)) == 0;
c) (v | (v – 1)) == 0;
d) (v & (v – 1)) == 0;
View Answer
Answer:d
Explanation: Power of two integers have a single set bit followed by unset bits.
4. What is the value of the following 8-bit integer after all statements are executed?
int x = 1;
x = x << 7;
x = x >> 7;
a) 1
b) -1
c) 127
d) Implementation defined
View Answer
Answer:d
Explanation: Right shift of signed integers is undefined, and has implementation-defined behaviour.
5. Which of these expressions will make the rightmost set bit zero in an input integer x?
a) x = x | (x-1)
b) x = x & (x-1)
c) x = x | (x+1)
d) x = x & (x+1)
View Answer
Answer:b
Explanation: None.
6. Which of these expressions will isolate the rightmost set bit?
a) x = x & (~x)
b) x = x ^ (~x)
c) x = x & (-x)
d) x = x ^ (-x)
View Answer
Answer:c
Explanation: None.
7. 0946, 786427373824, ‘x’ and 0X2f are _____, _____, ____ and _____ literals respectively
a) decimal, character,octal, hexadecimal
b) octal, hexadecimal, character, decimal
c) hexadecimal, octal, decimal, character
d) octal, decimal, character, hexadecimal
View Answer
Answer:d
Explanation: Literal integer constants that begin with 0x or 0X are interpreted as hexadecimal and the ones that begin with 0 as octal. The character literal are written within ”.
8. What will be the output of this program?
  1.     #include <iostream>
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         int a = 8;
  6.         cout << "ANDing integer 'a' with 'true' :" << a && true;
  7.         return 0;
  8.     }
a) ANDing integer ‘a’ with ‘true’ :8
b) ANDing integer ‘a’ with ‘true’ :0
c) ANDing integer ‘a’ with ‘true’ :1
d) None of the mentioned
View Answer
Answer:a
Explanation: None.
9. What will be output of this program?
  1.     #include <iostream>
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         int i = 3;
  6.         int l = i / -2;
  7.         int k = i % -2;
  8.         cout << l << k;
  9.         return 0;
  10.     }
a) compile time error
b) -1 1
c) 1 -1
d) implementation defined
View Answer
Answer:b
Explanation: Sign of result of mod operation on negative numbers is sign of the dividend.
10. What will be output of this function?
  1.     int main()
  2.     {
  3.         register int i = 1;
  4.         int *ptr = &i;
  5.         cout << *ptr;
  6.  return 0;
  7.     }
a) 0
b) 1
c) Compiler error may be possible
d) Runtime error may be possible
View Answer
Answer:c
Explanation: Using & on a register variable may be invalid, since the compiler may store the variable in a register, and finding the address of it is illegal.
FLOATING POINT TYPES


1. Which of the following is not one of the sizes of the floating point types?
a) short float
b) float
c) long double
d) double
View Answer
Answer:a
Explanation:Floating point types occur in only three sizes-float, long double and double.
2. Which of the following is a valid floating point literal?
a) f287.333
b) F287.333
c) 287.e2
d) 287.3.e2
View Answer
Answer:c
Explanation:To make a floating point literal, we should attach a suffix of ‘f’ or ‘F’ and there should not be any blank space.
3. What is the range of the floating point numbers?
a) -3.4E+38 to +3.4E+38
b) -3.4E+38 to +3.4E+34
c) -3.4E+38 to +3.4E+36
d) -3.4E+38 to +3.4E+32
View Answer
Answer:a
Explanation:None.
4. Which of three sizes of floating point types should be used when extended precision is required?
a) float
b) double
c) long double
d) extended float
View Answer
Answer:c
Explanation:Float for single precision, double for double precision and long double for extended precision.
5. What is the output of this program?
  1.     #include <iostream>
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         float num1 = 1.1;
  6.         double num2 = 1.1;
  7.         if (num1 == num2)
  8.            cout << "stanford";
  9.         else
  10.            cout << "harvard";
  11.         return 0;
  12.     }
a) harvard
b) stanford
c) compile time error
d) runtime error
View Answer
Answer:a
Explanation:Float store floating point numbers with 8 place accuracy and requires 4 bytes of Memory. Double has 16 place accuracy having size of 8 bytes.
Output:
$ g++ float3.cpp
$ a.out
harvard
6. What is the output of this program?
  1.     #include <iomanip>
  2.     #include <iostream>
  3.     using namespace std;
  4.     int main()
  5.     {
  6.         cout << setprecision(17);
  7.         double d = 0.1;
  8.         cout << d << endl;
  9.         return 0;
  10.     }
a) 0.11
b) 0.10000000000000001
c) 0.100001
d) compile time error
View Answer
Answer:b
Explantion:The double had to truncate the approximation due to it’s limited memory, which resulted in a number that is not exactly 0.1.
Output:
$ g++ float2.out
$ a.out
0.10000000000000001
7. What is the output of the following program?
  1.     #include <iostream>
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         float i = 123.0f;
  6.         cout << i << endl;
  7.         return 0;
  8.     }
a) 123.00
b) 1.23
c) 123
d) compile time error
View Answer
Answer:c
Explanation:The value 123 is printed because of its precision.
$ g++ float.cpp
$ a.out
123
8. Which is used to indicate single precision value?
a) F or f
b) L or l
c) either a or b
d) neither a or b
View Answer
Answer:a
Explanation:None.
9. What is the output of this program?
  1.     #include <iostream>
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         float f1 = 0.5;
  6.         double f2 = 0.5;
  7.         if (f1 == 0.5f)
  8.             cout << "equal";
  9.         else
  10.             cout << "not equal";
  11.         return 0;
  12.     }
a) equal
b) not equal
c) compile time error
d) runtime error
View Answer
Answer:a
Explanation:0.5f results in 0.5 to be stored in floating point representations.
Output:
$ g++ float.cpp
$ a.out
equal
10. Which is correct with respect to size of the datatypes?
a) char > int < float
b) int < char > float
c) char < int < float
d) char < int < double
Answer:d
Explanation:The char has lesser bytes than int and int has lesser bytes than double whereas int and float can potentially have same sizes.

SIZES

1. The size of an object or a type can be determined using which operator?
a) malloc
b) sizeof
c) malloc
d) calloc
View Answer
Answer:b
Explanation:The sizeof operator gives the size of the object or type.
2. It is guaranteed that a ____ has atleast 8bits and a ____ has atleast 16 bits.
a) int, float
b) char, int
c) bool, char
d) char, short
View Answer
Answer:d
Explanation:None.
3. Implementation dependent aspects about an implementation can be found in ____
a) <implementation>
b) <limits>
c) <limit>
d) <numeric>
View Answer
Answer:b
Explanation:The limit header holds the details of the machine dependent details.
4. Size of C++ objects are expressed in terms of multiples of the size of a ____ and the size of a char is ____.
a) char, 1
b) int, 1
c) float, 8
d) char, 4
View Answer
Answer:a
Explanation:None.
5. Identify the incorrect option.
a) 1 <= sizeof(bool) <= sizeof(long)
b) sizeof(float) <= sizeof(double) <= sizeof(long double)
c) sizeof(char) <= sizeof(long) <=sizeof(wchar_t)
d) sizeof(N) = sizeof(signed N) = sizeof(unsigned N)
Answer:c
Explanation:sizeof(char) <= sizeof(wchar_t) <= sizeof(long)

6. What is the output of the following program?
  1.     #include <iostream>
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         int num = 0x20 + 020 + 20;
  6.         cout << sizeof(num)<<'\n';
  7.         return 0;
  8.     }
a) 2
b) 4
c) Depends on compiler.
d) garbage
View Answer
Answer:c
Explanation:The sum of three numbers are belongs to different number systems, so the result is typecasted into integer.
Output:
$ g++ size.cpp
$ a.out
4
7. What is the output of the following program?
  1.     #include <iostream>
  2.     using namespace std;
  3.     int main ( )
  4.     {
  5.         static double i;
  6.         i = 20;
  7.         cout << sizeof(i);
  8.         return 0;
  9.     }
a) 4
b) 2
c) 8
d) garbage
View Answer
Answer:c
Explanation:The size of the double data type is 8.
$ g++ size1.cpp
$ a.out
8
8. What is the output of the following program?
  1.     #include <iostream>
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         int num1 = 10;
  6.         float num2 = 20;
  7.         cout << sizeof(num1 + num2);
  8.         return 0;
  9.     }
a) 2
b) 4
c) 8
d) garbage
View Answer
Answer:b
Explanation:In this program, integer is converted into float. Therefore the result of num1 and num2 is float. And it is returning the size of the float.
Output:
$ g++ size2.cpp
$ a.out
4
9. What is the output of the following program?
  1.     #include <iostream>
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         int a = 5;
  6.         float b;
  7.         cout << sizeof(++a + b);
  8.         cout << a;
  9.         return 0;
  10.     }
a) 2 6
b) 4 6
c) 2 5
d) 4 5
View Answer
Answer:d
Explanation:The a as a integer will be converted to float while calculating the size. The value of any variable doesn’t modify inside sizeof operator. Hence value of variable a will remain 5.
Output:
$ g++ size3.cpp
$ a.out
4 5
10. What would be the output of the following program (in 32-bit systems)?
  1.     #include <iostream>
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         cout << sizeof(char);
  6.         cout << sizeof(int);
  7.         cout << sizeof(float);
  8.         return 0;
  9.     }
a) 1 4 4
b) 1 4 8
c) 1 8 8
d) none of the mentioned
View Answer
Answer:a
Explanation:Character is 1 byte, integer 4 bytes and float 4 bytes.
VOID
1. Which of the following will not return a value?
a) null
b) void
c) empty
d) free
View Answer
Answer:b
Explanation:None.
2. ____ have the return type void?
a) all functions
b) constructors
c) destructors
d) none of the mentioned
View Answer
Answer:d
Explanation:Constructor creats an Object and Destructor destroys the object. They are not supposed to return anything, not even void.
3. What does the following statement mean?
    void a;
a) variable a is of type void
b) a is an object of type void
c) declares a variable with value a
d) flags an error
View Answer
Answer:d
Explantion:There are no void objects.
4. Choose the incorrect option
a) void is used when the function does not return a value.
b) void is also used when the value of a pointer is null.
c) void is used as the base type for pointers to objects of unknown type.
d) void is a special fundamental type.
View Answer
Answer:b
Explanation: void fundamental type is used in the cases of a and c.
5. What is the output of this program?
  1.     #include <iostream>
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         void a = 10, b = 10;
  6.         int c;
  7.         c = a + b;
  8.         cout << c;
  9.         return 0;
  10.     }
a) 20
b) compile time error
c) runtime error
d) none of the mentioned
View Answer
Answer:b
Explanation:void will not accept any values to its type.
ENUMERATIONS
1. Identify the incorrect option.
a) enumerators are constants
b) enumerators are user defined types
c) enumerators are same as macros
d) enumerator values start from 0 by default
View Answer
Answer:c
Explanation:enumerators are used in order to create our own types whereas macros are textual substitutions.
2. In which type does the enumerators are stored by the compiler?
a) string
b) integer
c) float
d) none of the mentioned
View Answer
Answer:b
Explanation:None.
3. To which of these enumerators can be assigned?
a) integer
b) negative
c) enumerator
d) all of the mentioned
View Answer
Answer:d
Explanation:Since enumerators evaluate to integers, and integers can be assigned to enumerators, enumerators can be assigned to other enumerators.
4. What will happen when defining the enumerated type?
a) it will not allocate memory
b) it will allocate memory
c) it will not allocate memory to its variables
d) none of the mentioned
View Answer
Answer:a
Explanation:Enumerator will allocate the memory when its variables are defined.
5. Which variable does equals in size with enum variable?
a) int variable
b) float variable
c) string variable
d) none of the mentioned
View Answer
Answer:a
Explanation:The enum variable are converted to integer and stored by compiler. So both are equal in size.
6. What is the output of this program?
  1.     #include <iostream>
  2.     using namespace std;
  3.     enum  cat {
  4.         temp = 7
  5.     };
  6.     int main()
  7.     {
  8.         int age = 14;
  9.         age /= temp;
  10.         cout << "If you were cat, you would be " << age << endl;
  11.         return 0;
  12.     }
a) If you were cat, you would be 5
b) If you were cat, you would be 2
c) If you were cat, you would be 7
d) none of the mentioned
View Answer
Answer:b
Explanation: The age will be divided by using compound assignment operator and so it will return the age of the cat according to your age.
$ g++ enum1.cpp
$ a.out
If you were cat, you would be 2
7. What is the output of this program?
  1.     #include <iostream>
  2.     using namespace std;
  3.     enum test {
  4.         A = 32, B, C
  5.     };
  6.     int main()
  7.     {
  8.         cout << A << B<< C;
  9.         return 0;
  10.     }
a) 323334
b) 323232
c) 323130
d) none of the mentioned
View Answer
Answer:a
Explanation:If we not assigned any value to enum variable means, then the next number to initialized number will be allocated to the variable.
Output:
$ g++ enum2.cpp
$ a.out
323334
8. What is the output of this program?
  1.     #include <iostream>
  2.     using namespace std;
  3.     enum colour {
  4.         green, red, blue, white, yellow, pink
  5.     };
  6.     int main()
  7.     {
  8.         cout << green<< red<< blue<< white<< yellow<< pink;
  9.         return 0;
  10.     }
a) 012345
b) 123456
c) compile time error
d) runtime error
View Answer
Answer:a
EXplanation:The enumerator values start from zero if it is unassigned.
Output:
$ g++ enum3.cpp
$ a.out
012345
9. What is the output of this program?
  1.     #include <iostream>
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         enum channel {star, sony, zee};
  6.         enum symbol {hash, star};
  7.         int i = 0;
  8.         for (i = star; i <= zee; i++) {
  9.             printf("%d ", i);
  10.         }
  11.         return 0;
  12.     }
a) 012
b) 123
c) compile time error
d) runtime error
View Answer
Answer:c
Explanation:enumartion variable ‘star’ appears two times in main() which causes the error. An enumaration constant must be unique within the scope.
10. What is output of the this program?
  1.     #include <iostream>
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         int i;
  6.         enum month {
  7.             JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC
  8.         };
  9.         for (i = MAR; i <= NOV; i++)
  10.             cout << i;
  11.         return 0;
  12.     }
a) 01234567891011
b) 123456789101112
c) 34567891011
d) 123456789
View Answer
Answer:c
Explanation:we are getting the values from march to november and printing its concern number.
DECLARATIONS
1. Choose the correct option.
    extern int i;
    int i;
a) both 1 and 2 declare i
b) 1 declares the variable i and 2 defines i
c) 1 declares and defines i, 2 declares i
d) 1 declares i,2 declares and defines i
View Answer
Answer:d
Explanation:The keyword extern is not a definition and is not allocated storage until it is initialized.
2. Pick the right option
    Statement 1:A definition is also a declaration.
    Statement 2:An identifier can be declared just once.
a) Statement 1 is true, Statement 2 is false.
b) Statement 2 is true, Statement 1 is false.
c) Both are false.
d) Both are true.
View Answer
Answer: b
Explanation:An identifier can be declared many times must be defined just once.
3. Which of the given statements are false.
1. extern int func;
2. extern int func2(int,int);
3. int func2(int,int);
4. extern class foo;
a) 3 and 4 only
b) 2 and 3 only
c) only 4
d) 2, 3 and 4
View Answer
Answer:c
Explanation:No extern are allowed for class declarations.
4. Pick the right option
    Statement 1:Global values are not initialized by the stream.
    Statement 2:Local values are implicitly initialised to 0.
a) Statement 1 is true, Statement 2 is false.
b) Statement 2 is true, Statement 1 is false.
c) Both are false.
d) Both are true.
View Answer
Answer:c
Explanation:Global values are implicitly initialised to 0, but local values have to be initialised by the system.
5. What is the output of this program?
  1.     #include <iostream>
  2.     using namespace std;
  3.     int g = 100;
  4.     int main()
  5.     {
  6.         int a;
  7.         {
  8.             int b;
  9.             b = 20;
  10.             a = 35;
  11.             g = 65;
  12.            cout << b << a << g;
  13.         }
  14.         a = 50;
  15.         cout << a << g;
  16.         return 0;
  17.     }
a) 2035655065
b) 2035655035
c) 2035635065
d) none of the mentioned
View Answer
Answer:a
Explanation:The local values of a and g within the block are more dominant than the global values.
Output:
$ g++ dec1.cpp
$ a.out
2035655065
6. Can two functions declare variables(non static) with the same name.
a) No
b) Yes
c) Yes, but not a very efficient way to write programs.
d) No, it gives a runtime error.
View Answer
Answer:c
Explanation:We can declare variables with the same name in two functions because their scope lies within the function.
7. What is the output of this program?
  1.     #include <iostream>
  2.     using namespace std;
  3.     void addprint()
  4.     {
  5.         static int s = 1;
  6.         s++;
  7.         cout << s;
  8.     }
  9.     int main()
  10.     {
  11.         addprint();
  12.         addprint();
  13.         addprint();
  14.         return 0;
  15.     }
a) 234
b) 111
c) 123
d) 235
View Answer
Answer:a
Explanation:The variable that is declared as static has a file scope.
Output:
$ g++ dec2.cpp
$ a.out
234
8. What is the output of this program?
  1.     #include <iostream>
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         int a = 10;
  6.         if (a < 10) {
  7.             for (i = 0; i < 10; i++)
  8.                cout << i;
  9.         }
  10.         else {
  11.             cout << i;
  12.         }
  13.         return 0;
  14.     }
a) 0123456789
b) 123456789
c) 0
d) error
View Answer
Answer:d
Explanation:We will get compilation error because ‘i’ is an undeclared identifier.
9. Identify the incorrect statements.
    int var = 10;
    int *ptr = &(var + 1); //statement 1
    int *ptr2 = &var; //statement 2
    &var = 40; //statement 3
a) Statement 1 and 2 are wrong
b) Statement 2 and 3 are wrong
c) Statement 1 and 3 are wrong
d) All the three are wrong
View Answer
Answer:c
Explanation:In statement 1 lvalue is required as unary ‘&’ operand and in statement 3 lvalue is required as left operand.
10. Identify the type of the variables.
    typedef char* CHAR;
    CHAR p,q;
a) char*
b) char
c) CHAR
d) unknown
View Answer
Answer:a
Explanation: The statement makes CHAR a synonym for char*.
Previous Post Next Post