Tokens in C++ MCQs: 11 Solved Questions on Identifiers, Operators and Whitespace

Solve 11 real exam and placement-test C++ token questions, then learn the small rules that settle identifier, whitespace, operator, scope and output traps.

KnowledgeGate Team

Exam prep & CS education

15 Aug 20268 min read

Token questions can look like vocabulary tests, but the marks are usually lost while separating a keyword from an identifier, whitespace from punctuation, or stream insertion from bit shifting. One method settles almost all of them: classify the word or symbol, apply the smallest relevant C++ rule, then trace the exact token sequence before choosing an option. Write that rule beside every question you get wrong, because the same eight rules keep returning: identifier characters, control-string classes, whitespace, statement terminators, scope resolution, compound assignment, precedence and stream insertion. The wider Coding & DSA courses for placements carry the surrounding learning path.

Tokens in C++: classify the building blocks before solving

A token is the smallest meaningful lexical unit seen by the compiler. In C++ the practical families are keywords, identifiers, literals, operators and punctuators. Whitespace and comments behave differently: whitespace normally separates tokens, while comments disappear during translation rather than becoming ordinary program tokens.

Take the statement int total_2 = 7 + 5;. Its seven meaningful tokens are:

Source lexeme

Token class

int

Keyword

total_2

Identifier

=

Assignment operator

7

Integer literal

+

Arithmetic operator

5

Integer literal

;

Punctuator and statement terminator

The spaces only separate those tokens. In the language of tokens and lexemes in lexical analysis, source character sequences such as total_2 are lexemes, while identifier is the corresponding token class.

C++ identifiers and names: valid characters and scope

A standard C++ identifier starts with a letter or underscore, then uses letters, digits or underscores, and cannot be a reserved keyword. Scope is separate: one name can exist at multiple levels, and qualification selects a declaration.

Q1. A valid C++ identifier

Asked in: Hexaware 2025, CoCubes 2025.

Which of the following is the correct identifier?

  • (A) $var_name

  • (B) VAR_123

  • (C) varname@

  • (D) None of the above

Answer: (B) VAR_123.

Read it character by character: V, A, R, _, 1, 2, 3. It starts with a letter and all later characters are allowed. $var_name and varname@ contain non-standard identifier characters.

Q2. Global scope resolution with a shadowed name

Asked in: Hexaware 2025, CoCubes 2025.

What is the output of the given program?

#include <iostream>
using namespace std;

int x = 10;

void fun()
{
    int x = 2;
    {
        int x = 1;
        cout << ::x << endl;
    }
}

int main()
{
    fun();
    return 0;
}
  • (A) 1

  • (B) 2

  • (C) 10

  • (D) error

Answer: (C) 10.

The declarations are global x = 10, function-local x = 2, and block-local x = 1. An unqualified inner x would select 1, but ::x selects the global name. The stream therefore prints 10, then endl adds a newline.

C++ control strings and whitespace: symbols versus separators

A formatted control string can contain conversion specifications such as %d, whitespace, and non-whitespace literal characters. In "%d, %d", the two %d sequences are format specifiers, the comma is a literal, and the single space is whitespace. Separate those three roles first and the classification questions stop being guesswork.

Q3. Components of a control string

Asked in: UGC NET 2014.

The control string in C++ consists of three important classifications of characters

  • (A) Escape sequence characters, Format specifiers and Whitespace characters

  • (B) Special characters, White-space characters and Non-white space characters

  • (C) Format specifiers, White-space characters and Non-white space characters

  • (D) Special characters, White-space characters and Format specifiers

Answer: (C).

Conversion specifications direct value conversion, whitespace controls spacing, and other non-whitespace characters are literals. The example %d, %d contains all three: two specifiers, a space, and a comma.

Q4. Recognising a non-whitespace character

Asked in: TPSC 2026.

Which of the following is not a white space character ?

  • (A) BLANK

  • (B) TAB

  • (C) SEMICOLON

  • (D) NEWLINE

Answer: (C) SEMICOLON.

Blank, tab and newline are whitespace used for separation or layout. A semicolon is a visible punctuator that terminates a statement, so it is not whitespace.

Q5. What formatted extraction skips

Asked in: TPSC 2026.

EXTRACTION operator >> ignores the following

  • (A) INTEGER

  • (B) FLOAT

  • (C) WHITESPACE

  • (D) COMMENTS

Answer: (C) WHITESPACE.

With default formatted input, cin >> value skips leading spaces, tabs and newlines. For " 42", int n; cin >> n; skips three spaces and stores 42. Comments disappear from source during translation, while streams process runtime characters; noskipws disables default whitespace skipping.

C++ operators: compound assignment, scope and precedence

For operator questions, substitute a small value, expand compound assignments, and compare precedence only among the listed choices.

Q6. Compound assignment equivalence

Asked in: KVS 2018.

Which of the following is not correct in C++?

  • (A) x -= 2; is the same as x = x - 2;

  • (B) x *= 2; is the same as x = x * 2;

  • (C) x %= 2; is the same as x = x/2

  • (D) x /= 2; is the same as x = x/2

Answer: (C).

The % operator computes a remainder, so x %= 2 means x = x % 2, not division. Starting independently from integer x = 7, subtraction gives 5, multiplication gives 14, remainder gives 1, and division gives 3. Thus 7 % 2 = 1 and integer 7 / 2 = 3, proving (C) false.

Q7. Qualifying the base-class function

Asked in: DSSSB 2018.

Which of the following is used to access the overridden function of the base class from the derived class?

  • (A) ::

  • (B) <<

  • (C) >>

  • (D) :

Answer: (A) ::.

Inside a derived-class member, Base::show() explicitly names the base implementation. << and >> can be shift or stream operators, while a single colon does not qualify a name.

Q8. Highest precedence among the listed operators

Asked in: IBPS 2023.

Which of the following operators has the highest precedence in C++?

  • (A) Addition (+)

  • (B) Logical AND (&&)

  • (C) Multiplication (*)

  • (D) Function call operator ()

  • (E) Assignment (=)

Answer: (D) Function call operator ().

Among the options, a function call binds more tightly than multiplication, addition, logical AND and assignment. If int f(){ return 4; }, then 2 + 3 * f() gives f() = 4, 3 * 4 = 12, then 2 + 12 = 14. Scope resolution sits above the postfix group in a complete table, but it is not listed.

C++ statement and stream tokens: semicolon, insertion and output

Here, ; terminates a statement, while << inserts a value into an output stream. A chain of insertions emits exactly the characters it is given and adds no separators, so the output has to be traced character by character.

Q9. The C++ statement terminator

Asked in: DSSSB 2018.

Which of the following characters is a terminal that terminates a statement in C++?

  • (A) Semicolon (;)

  • (B) Colon (:)

  • (C) Ampersand (&)

  • (D) Dollar ($)

Answer: (A) Semicolon (;).

A semicolon terminates ordinary declaration and expression statements. Colon and ampersand have different roles, while dollar has no standard punctuator role in C++.

Q10. The stream insertion operator

Asked in: DSSSB 2018.

Which of the following is the insertion operator in C++, used to write formatted data into the stream?

  • (A) >>

  • (B) <<

  • (C) &&

  • (D) <>

Answer: (B) <<.

In cout << value, << inserts a formatted value into the output stream. In input-stream use, >> extracts; && is logical AND; and <> is not one C++ operator.

Q11. Trace a chain of insertion operators exactly

Asked in: BPSC 2023.

What will be the output of the following program?

 int a = 5;
 cout << "FIRST " << a<<2 << "SECOND";
  • (A) FIRST 52 SECOND

  • (B) FIRST 20 SECOND

  • (C) SECOND 25 FIRST

  • (D) More than one of the above

  • (E) None of the above

Answer: (E) None of the above.

Trace the left-associative chain: "FIRST " emits FIRST , a emits 5, the literal 2 emits 2, and "SECOND" emits SECOND without a separator. The exact output is FIRST 52SECOND, absent from the options because (A) adds a space. Source spacing around a<<2 does not make this a numeric shift, since the left operand is the stream returned by the previous insertion.

Tokens in C++ exam traps: a five-step checking routine

Use the same routine on every token MCQ:

  1. Identify the token class.

  2. Check identifier or keyword rules.

  3. Separate whitespace from punctuators.

  4. Apply the operator's meaning and precedence.

  5. Trace exact output without adding spaces.

Three quick checks expose the common traps. VAR_123 is valid, but $var_name is not portable. 7 % 2 = 1, while integer 7 / 2 = 3. cout << 5 << 2 prints 52, not 20 and not 5 2. Each question here has exactly one correct option, so after applying the rule or completing the trace, commit to a single choice. The guide to MCQ, MSQ or NAT question types is a useful format reminder.

The short version and the next C++ practice step

Identifiers obey character and keyword rules. Whitespace normally separates tokens, while ;, ::, << and compound assignments each have a distinct job. Precedence decides binding, and exact stream output contains only the characters actually emitted.

Now reattempt all 11 questions without looking at the answers, and write one rule beside every miss. That written correction turns a guessed answer into a rule you can reuse. On a second pass, trace Q2, Q6, Q8 and Q11 on paper because their scope, arithmetic, precedence and output reasoning transfer to many other problems. If you want the language sequence and practice organised together, the C Language course: concepts, MCQs and coding questions teaches the same identifier, operator and precedence rules and adds an object-oriented section, and C++ Tutorial: The Complete Learning Path from First Program to STL shows where tokens sit in the rest of the language. Keep the five-step routine beside you until classification and exact tracing become automatic.