C# Programming - Classes and Objects - Discussion

Discussion :: Classes and Objects - General Questions (Q.No.1)

1. 

Which of the following statements is correct about the C#.NET code snippet given below?

class Student s1, s2; // Here 'Student' is a user-defined class.
s1 = new Student(); 
s2 = new Student();

[A]. Contents of s1 and s2 will be exactly same.
[B]. The two objects will get created on the stack.
[C]. Contents of the two objects created will be exactly same.
[D]. The two objects will always be created in adjacent memory locations.
[E]. We should use delete() to delete the two objects from memory.

Answer: Option C

Explanation:

No answer description available for this question.

G.Sriram said: (Aug 12, 2011)  
No issue.

A default constructor with dynamic allocation of objects is created a default constructor accepts no parameters. So the constructors that are created will have same characteristics.

Sundar said: (Jan 3, 2012)  
@G.Sriram

What if the constructor assigns random values to the member variables of the instance of the class ?

Paurush said: (Apr 27, 2012)  
What is the differnce b/w option A and C ?
Please clarify the solution.

Indrapal said: (Apr 30, 2012)  
I do not think compiler will assign the content to both object and I tried to VS2010 and show that objects has the different content
BaseClass s1,s2;// = new Drived();
s1 = new BaseClass();
s2 = new BaseClass();
if (s1 == s2)
{
Console.WriteLine("contents of both objects are same");
}

Kanika said: (Jun 26, 2012)  
In terms of memory allocated to s1 and s2 they are equal i.e.
All objects created from a class will occupy equal number of bytes in memory.

Mayank Srivastava said: (Apr 5, 2013)  
I am agree with Indrapal. I have also tried it and it shows that objects has the different content.

Naresh said: (Jun 20, 2013)  
@Indrapal.

s1 and s2 contains the address that where the content is stored.

So s1 and s2 are stored at diff memory locations so obviously if block won't execute.

@Paurush.

Exactly same means including addresses of the objects,
But its not true as I explained above,
So the answer is C only.

Vaiju said: (Aug 2, 2013)  
s1 and s2 contains the address that where the content is stored.

So s1 and s2 are stored at diff memory locations so obviously .

Andrue Cope said: (Oct 27, 2013)  
Yuk, sneaky question :)

I think a lot of C# programmers will assume that 'the contents of s1' mean the object pointed to it. I did. Have to say that it's more clear if you rewrite in C++ :D

Andrew Cope said: (Apr 3, 2014)  
Slightly sneaky indeed. It shouldn't say 'Contents of..exactly the same'. It would be more correct to say 'The characteristics of the two objects will be the same'.

That covers off the scenario mentioned above where member data is being initialised to random or otherwise variant values.

Manish Kumar said: (Feb 17, 2015)  
@Andrew Cope.

I agree its better to say "The characteristics of the two objects will be the same".

Alek said: (May 5, 2015)  
This is wrong, for example if Student is defined as:

class student
{
static int _last_id;
static Student () { _last_id = 0; }
int id;
student ()
{
id = _last_id;
_last_id++;
}
}

Then obviously their contents won't be the same.
The correct answer is : 'Program won't compile'.
Because keyword class is illegal in a statement.

John said: (May 10, 2015)  
@Alek.

Yes same thoughts here, the code given won't compile, because the class keyword is used before the declaration of the 2 student objects s1, s2.

Sidhu said: (Sep 4, 2015)  
BaseClass s1,s2;// = new Drived();
s1 = new BaseClass();
s2 = new BaseClass();
if (s1 == s2)
{
Console.WriteLine("contents of both objects are same");
}

Gardenexit said: (Jun 1, 2016)  
@Indrapal.

s1 and s2 have different hashcodes.
So,
s1 == s2
It can never be true.

Dickenz said: (Feb 22, 2017)  
A: S1 and S2 are merely reference variables containing the addresses of the 2 objects created therefor contents are different.

B: reference type variables are created on the heap not stack.

D: objects created on the heap are allocated memory at run time such that they are not necessarily created in adjacent memory.

E: deleting from memory is done by garbage collection or explicitly defining a despose method using methods such as GC.SuppressFinalize.

Monk said: (Apr 13, 2017)  
class Student s1, s2;

Can anyone explain what is s1,s2 here, whether is it class or variable?

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.