arrow
arrow
arrow
Which of the following is a correct syntax for defining a class in C++?
Question



Which of the following is a correct syntax for defining a class in C++?

A.

class B;

B.

public class A{}

C.

class B{}; public class A{}

D.

class A { int x; };

E.

ObjectA(int x);

Correct option is D

In C++, the correct syntax for defining a class includes the class keyword followed by the class name, curly braces containing class members, and a semicolon at the end. Thus, class A { int x; }; is the correct syntax for defining a class with a member variable x.
Important Key Points:
1. Syntax Structure: In C++, the class keyword is used to define a class, followed by the class name and the body enclosed in {}.
2. Semicolon Requirement: C++ requires a semicolon ; at the end of the class definition.
3. Member Variables: Class members like variables and functions are declared within the class body.
Knowledge Booster:
· class B;: Missing the class body and semicolon at the end; incomplete class definition.
· public class A{}: In C++, public is used as an access specifier within the class, not before the class keyword as in some other languages.
· class B{}; public class A{}: Incorrect because C++ does not use public before the class keyword in the way that languages like Java do.
· ObjectA(int x);: This appears as a constructor declaration, not a class definition.

Free Tests

Free
Must Attempt
Video Solutions

RBI Assistant Pre 2026 Full Mock Test -01

languageIcon English
  • pdpQsnIcon100 Questions
  • pdpsheetsIcon100 Marks
  • timerIcon60 Mins
languageIcon English
Free
Must Attempt
Video Solutions

RBI Asst Prelims 2026 : Reasoning Section Test 01

languageIcon English
  • pdpQsnIcon35 Questions
  • pdpsheetsIcon35 Marks
  • timerIcon20 Mins
languageIcon English
Free
Must Attempt
Video Solutions

RBI Office Attendant 2026 Full Mock Test - 01

languageIcon English
  • pdpQsnIcon120 Questions
  • pdpsheetsIcon120 Marks
  • timerIcon90 Mins
languageIcon English
test-prime-package

Access ‘IBPS SO IT Officer’ Mock Tests with

  • 60000+ Mocks and Previous Year Papers
  • Unlimited Re-Attempts
  • Personalised Report Card
  • 500% Refund on Final Selection
  • Largest Community
students-icon
354k+ students have already unlocked exclusive benefits with Test Prime!
Our Plans
Monthsup-arrow