Question 1Medium
Which of the following statements are correct about an interface used in C#.NET?
I. An interface can contain properties, methods and events.
II. The keyword must implement forces implementation of an interface.
III. Interfaces can be overloaded.
IV. Interfaces can be implemented by a class or a struct.
V. Enhanced implementations of an interface can be developed without breaking existing code.
- 1, 2
- 1, 4, 5
- 3, 4
- 3 only
Answer: B. 1, 4, 5
Explanation
A n interface contains only the methods, properties, events or indexers. A class or struct that implements the interface must implement the members of the interface that are specified in the interface
Step-by-step solution
- I is true: interfaces hold properties, methods and events.
- II is false: there is no 'must implement' keyword.
- III is false: interfaces are not overloaded in this sense.
- IV and V are true: classes and structs can implement them, and implementations can be enhanced without breaking code.
- So option B (1, 4, 5) is correct.
Why the other options are wrong
- A. II is false.
- C. III is false, and it leaves out I.
- D. III alone is false.
Tip: Interfaces can be implemented by classes and structs.
Question 2Medium
What will be the output for given set of code?
namespace ConsoleApplication4
{
abstract class A
{
int i;
public abstract void display();
}
class B: A
{
public int j;
public override void display()
{
Console.WriteLine(j);
}
}
class Program
{
static void Main(string[] args)
{
B obj = new B();
obj.j = 2;
obj.display();
Console.ReadLine();
}
}
}
- 0.
- 2
- Compile time error.
- 1
Answer: B. 2
Explanation
Here in abstract class abstract method display() is declared and its full implementation i.e definition is given in subclass of class
Output :2
Step-by-step solution
- Class B inherits the abstract class A and overrides display().
- In Main, obj.j is set to 2.
- obj.display() runs B's override, which prints the value of j.
- The output is 2, which is option B.
Why the other options are wrong
- A. Nothing sets j to 0 before the call, so 0 is not printed.
- C. The code compiles: the abstract method is overridden in B.
- D. The value 1 is never assigned anywhere.
Tip: Trace the object's field value at the moment the method is called.
Question 3Medium
What will be the output of given code snippet?
interface calc
{
void cal(int i);
}
class displayA : calc
{
public int x;
public void cal(int i)
{
x = i * i;
}
}
class displayB : calc
{
public int x;
public void cal(int i)
{
x = i / i;
}
}
class Program
{
public static void Main(string[] args)
{
displayA arr1 = new displayA();
displayB arr2 = new displayB();
arr1.x = 0;
arr2.x = 0;
arr1.cal(2);
arr2.cal(2);
Console.WriteLine(arr1.x + " " + arr2.x);
Console.ReadLine();
}
}
- 0 0
- 2
- 4 1
- 1 4
Answer: C. 4 1
Explanation
class display A executes the interface calculate by doubling the value of item similarly class display B implements the interface by dividing item by item. So, variable x of class display
A stores 4 and variable x of class display B
stores 1.
Output: 4, 1
Step-by-step solution
- The class headers in the question are garbled; read A and B as classes that implement the calc interface.
- arr1.cal(2) sets x = 2 × 2 = 4 in class A.
- arr2.cal(2) sets x = 2 ÷ 2 = 1 in class B.
- Printing arr1.x and arr2.x gives 4 1, so the answer is option C (4 1).
Why the other options are wrong
- A. Cal changes both values, so 0 0 is wrong.
- B. The output has two numbers, not one.
- D. A gives 4 and B gives 1, so the order 1 4 is wrong.
Tip: Trace each object's own method call separately; the same method can do different maths in different classes.
Question 4Medium
Which of the following modifier is used when an abstract method is redefined by a derived class?
- Overloads
- Override
- Base
- Virtual
Answer: B. Override
Explanation
The override modifier is required to extend or modify the abstract or virtual implementation of an inherited method, property, indexer, or event. An override method provides a new implementation of a member that is inherited from a base class. The method that is overridden by an override declaration is known as the overridden base method.
Step-by-step solution
- Override modifier is used to redefine abstract methods in derived classes.
- It allows changing or adding functionality to inherited abstract methods.
- Derived class must use override to implement abstract method from base class.
- Other modifiers like virtual are for non-abstract methods only.
- Correct answer is Override as it redefines the abstract method.
Why the other options are wrong
- A. Overloads are used for method overloading, not redefining abstract methods.
- C. Base is used to access base class members, not redefine them.
- D. Virtual is used for non-abstract methods, not abstract ones.
Tip: Use override when redefining abstract methods in inheritance.
Question 5Medium
What will be the output for given set of code?
namespace ConsoleApplication4
{
abstract class A
{
public int i;
public abstract void display();
}
class B : A
{
public int j;
public int sum;
public override void display()
{
sum = i + j;
Console.WriteLine(+i + "\n" + +j);
Console.WriteLine("sum is:" + sum);
}
}
class Program
{
static void Main(string[] args)
{
A obj = new B();
obj.i = 2;
B obj1 = new B();
obj1.j = 10;
obj.display();
Console.ReadLine();
}
}
}
- 2, 10,12.
- 0, 10, 10.
- 2, 0, 2
- 0, 0, 0.
Answer: C. 2, 0, 2
Explanation
Abstract method implementation processed in subclass B also the object obj of abstract class A initialize value of i as 2.
The object of class B also intialize value of j as 10.
Since, the method display() is called using object of class A which is obj and hence i = 2 whereas j = 0.
So, sum = 2.
Output: 2 0
sum is: 2
Step-by-step solution
- Obj has type A and holds a B object, so obj.i = 2 sets i to 2.
- obj1.j = 10 sets j on a different object, obj1, not on obj.
- obj.display() runs B's override: sum = i + j = 2 + 0 = 2, because obj.j is 0.
- It prints 2 and 0 on separate lines, then sum is:2, so the answer is option C (2, 0, 2).
Why the other options are wrong
- A. J is 0 for obj, not 10, and sum is 2, not 12.
- B. I is 2, not 0, because obj.i was set to 2.
- D. I is 2 and sum is 2, so the output is not all zeros.
Tip: With a base-class variable, the object's own fields are used; a field set on another object does not change it.
Question 6Medium
Which of the following statements is correct about an interface used in C#.NET?
- One class can implement only one interface.
- In a program if one class implements an interface then no other class in the same program can implement this interface.
- From two base interfaces a new interface cannot be inherited.
- Properties can be declared inside an interface.
Answer: D. Properties can be declared inside an interface.
Explanation
Properties can be used inside an interface. In the above example theaccessor of an interface property does not have a body. Thus, the purpose of the accessors is to indicate whether the property isread-write, read-only, or write-only.
public interface ISampleInterface
{
// Property declaration:
string Name
{
get;
set;
}
}
Step-by-step solution
- A C# class can implement more than one interface, so option A is false.
- Many classes can implement the same interface, so option B is false.
- An interface can inherit from two or more base interfaces, so option C is false.
- Properties can be declared inside an interface, so the answer is option D.
Why the other options are wrong
- A. A class can implement more than one interface.
- B. Many classes can implement the same interface.
- C. An interface can inherit from several base interfaces.
Tip: An interface declares members (methods, properties, events) with no body; a class supplies the code.