From the following code (written using C++-style class syntax) determine the…

2013

From the following code (written using C++-style class syntax) determine the attributes of the class student: class student { string name; int marks; }; public static void main() { student S1 = new student(); student S2 = new student(); }

  1. A.

    Only name

  2. B.

    Both name and marks

  3. C.

    Only S1

  4. D.

    Both S1 and S2

Attempted by 475 students.

Show answer & explanation

Correct answer: B

Concept: In object-oriented programming, the attributes (also called fields or member/data variables) of a class are the variables declared directly inside the class body — they define the state every object of that class will hold. Creating an object from the class (instantiation) does not add new attributes; the object only receives its own copy of the already-declared fields.

Application: Walking through the given code —

  1. The class student's body declares two member variables: name of type string, and marks of type int. These two declarations are the class's attributes.

  2. The main() function then creates S1 and S2 using new student(). Each of these is a separate object (instance) of the class, each carrying its own copy of the name and marks fields — but S1 and S2 are objects, not attributes.

Cross-check: an attribute must be a type + identifier declaration written directly inside the class body, terminated by a semicolon. The two lines inside student{...} satisfy exactly this pattern. S1 and S2, by contrast, appear inside main(), assigned via new student() — the syntax for creating an object, not for declaring a class member — confirming they are instances rather than attributes.

Hence, the class student has two attributes: name and marks.

Note: this snippet's class-declaration style — a lowercase string type (as used with C++'s std::string / using namespace std) and a semicolon after the class body — is a stylistic convention associated with C++, not Java, where the analogous type is capitalized String and a semicolon after a class body is unusual (though not a hard error) style. The main()/object-creation lines borrow Java-style phrasing and, taken literally, would need adjustment for a strict compiler in either language (e.g. a pointer type for the new call in C++) — but that does not change which fields the class itself declares as attributes.

A video solution is available for this question — log in and enroll to watch it.

Explore the full course: Uppsc Polytechnic Lecturer 2025 Cs

Loading lesson…