C# - Classes and Objects
C# - Classes and Objects
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();
class Student s1, s2; // Here 'Student' is a user-defined class.
s1 = new Student();
s2 = new Student();
- Contents of s1 and s2 will be exactly same.
- The two objects will get created on the stack.
- Contents of the two objects created will be exactly same.
- The two objects will always be created in adjacent memory locations.
2. Which of the following statements is correct about the C#.NET code snippet given below?
class Sample
{
private int i;
public Single j;
private void DisplayData()
{
Console.WriteLine(i + " " + j);
}
public void ShowData()
{
Console.WriteLine(i + " " + j);
}
}
class Sample
{
private int i;
public Single j;
private void DisplayData()
{
Console.WriteLine(i + " " + j);
}
public void ShowData()
{
Console.WriteLine(i + " " + j);
}
}
- j cannot be declared as public.
- DisplayData() cannot be declared as private.
- DisplayData() cannot access j.
- ShowData() cannot access to i.
3. Which of the following statements are correct?
Instance members of a class can be accessed only through an object of that class.
A class can contain only instance data and instance member function.
All objects created from a class will occupy equal number of bytes in memory.
A class can contain Friend functions.
A class is a blueprint or a template according to which objects are created.
Instance members of a class can be accessed only through an object of that class.
A class can contain only instance data and instance member function.
All objects created from a class will occupy equal number of bytes in memory.
A class can contain Friend functions.
A class is a blueprint or a template according to which objects are created.
- 1, 3, 5
- 2, 4
- 3, 5
- 2, 4, 5
4. Which of the following statements is correct?
- Procedural Programming paradigm is different than structured programming paradigm.
- Object Oriented Programming paradigm stresses on dividing the logic into smaller parts and writing procedures for each part.
- Classes and objects are corner stones of structured programming paradigm.
- Object Oriented Programming paradigm gives equal importance to data and the procedures that work on the data.
5. Which of the following is the correct way to create an object of the class Sample?
Sample s = new Sample();
Sample s;
Sample s; s = new Sample();
s = new Sample();
Sample s = new Sample();
Sample s;
Sample s; s = new Sample();
s = new Sample();
- 1, 3
- 2, 4
- 1, 2, 3
- 4, 5
6. The this reference gets created when a member function (non-shared) of a class is called.
- TRUE
- FALSE
7. Which of the following statements are correct?
Data members ofa class are by default public.
Data members of a class are by default private.
Member functions ofa class are by default public.
A private function of a class can access a public function within the same class.
Member function of a class are by default private.
Data members ofa class are by default public.
Data members of a class are by default private.
Member functions ofa class are by default public.
A private function of a class can access a public function within the same class.
Member function of a class are by default private.
- 1, 3, 5
- 1, 4
- 2, 4, 5
- 1, 2, 3
8. Which of the following statements are correct about the C#.NET code snippet given below?
sample c;
c = new sample();
It will create an object called sample.
It will create a nameless object of the type sample.
It will create an object of the type sample on the stack.
It will create a reference c on the stack and an object of the type sample on the heap.
It will create an object of the type sample either on the heap or on the stack depending on the size of the object.
sample c;
c = new sample();
It will create an object called sample.
It will create a nameless object of the type sample.
It will create an object of the type sample on the stack.
It will create a reference c on the stack and an object of the type sample on the heap.
It will create an object of the type sample either on the heap or on the stack depending on the size of the object.
- 1, 3
- 2, 4
- 3, 5
- 4, 5
9. Which of the following statements is correct about the C#.NET code snippet given below?
int i;
int j = new int();
i = 10;
j = 20;
String str;
str = i.ToString();
str = j.ToString();
int i;
int j = new int();
i = 10;
j = 20;
String str;
str = i.ToString();
str = j.ToString();
- This is a perfectly workable code snippet.
- Since int is a primitive, we cannot use new with it.
- Since an int is a primitive, we cannot call the method ToString() using it.
- i will get created on stack, whereas j will get created on heap.
10. Which of the following statements are correct about the this reference?
this reference can be modified in the instance member function of a class.
Static functions of a class never receive the this reference.
Instance member functions of a class always receive a this reference.
this reference continues to exist even after control returns from an instance member function.
While calling an instance member function we are not required to pass the this reference explicitly.
this reference can be modified in the instance member function of a class.
Static functions of a class never receive the this reference.
Instance member functions of a class always receive a this reference.
this reference continues to exist even after control returns from an instance member function.
While calling an instance member function we are not required to pass the this reference explicitly.
- 1, 4
- 2, 3, 5
- 3, 4
- 2, 5