C# - Interfaces Questions and Answers
C# Interfaces programming questions and answers are frequently asked in technical interviews and placement exams conducted by top companies like Tech Mahindra, Infosys, and Wipro. Interfaces in C# define a contract that classes and structs can implement, ensuring consistency and abstraction in code design. Understanding interfaces helps programmers achieve loose coupling and multiple inheritance in an object-oriented way. Practicing programming interview questions with answers based on interfaces improves one’s ability to design modular, scalable software systems. Candidates should explore interface inheritance, explicit implementation, and default methods introduced in modern C# to excel in interview coding rounds.
Questions on interfaces and abstraction in C. Complement with C++ polymorphism and encapsulation
C# - Interfaces
21. Which of the following can implement an interface
- Class only
- Struct only
- Explanation: Both classes and structs can implement interfaces.
- Interface only
22. What happens if a class does not implement all interface members
- Code compiles successfully
- Runtime error occurs
- Explanation: All interface members must be implemented, otherwise a compile-time error occurs.
- Interface methods are ignored
23. Which feature is mainly achieved using interfaces
- Encapsulation
- Explanation: Interfaces support polymorphism by allowing different implementations of the same contract.
- Data hiding
- Memory management
24. Can an interface contain properties in C#
- No
- Yes, with implementation
- Explanation: Interfaces can declare properties but cannot implement them.
- Only static properties
25. Which version of C# introduced default interface methods
- C# 5.0
- C# 6.0
- C# 7.0
- Explanation: Default method implementations in interfaces were introduced in C# 8.0.
26. What keyword is used to explicitly implement an interface member
- explicit
- override
- interface
- Explanation: Explicit interface implementation uses the interface name, not a keyword.
27. Why is explicit interface implementation used
- To improve performance
- Explanation: It is used to hide interface members from direct access through the class object.
- To create objects
- To avoid inheritance
28. Can an interface contain fields in C#
- Yes
- Explanation: Interfaces cannot contain fields.
- Only readonly fields
- Only static fields
29. Which statement about interfaces is correct
- Interfaces can be instantiated
- Interfaces support constructors
- Explanation: Interfaces define a contract that implementing classes must follow.
- Interfaces store data
30. How many interfaces can a class implement
- Only one
- Only two
- Explanation: A class can implement multiple interfaces.
- None