Interface in .NET - placement questions

Interface in .NET - placement questions


Q.1 Trace the output of given below code.

using System;
interface IDemo
{
int x = 10;
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Welcome at careerRide");
}
}

A) Welcome at careerRide
B) Compile time error.
C) Run time error
D) None of the above.

View Answer / Hide Answer

ANSWER: B

Explanation:

If you run the above code you will get the compile time error as follows.
Interfaces cannot contain fields. So you cannot declare variable in interface.




Q. 2

using System;
interface IDemo
{
void myFunction();
}
class Program:IDemo
{
static void Main(string[] args)
{
myFunction();
}
void myFunction()
{
Console.WriteLine("Welcome at careerRide.com");
}
}

A) Compile time error.
B) Welcome at careerRide.com.
C) Run time error
D) None of the above.

View Answer / Hide Answer

ANSWER: A

Explanation:

In the above code Main is static function and static function can call only static function. But still if you want to call myFunction() then you have to create the object of the class.

using System;
interface IDemo
{
void myFunction();
}
class Program:IDemo
{
static Program p = new Program();
static void Main(string[] args)
{
p.myFunction();
}
public void myFunction()
{
Console.WriteLine("Welcome at careerRide.com");
}

}




Q.3 Which of the following can be declared (without implementation) in an interface?

A) events, indexers, methods, and properties.
B) Only methods
C) Only methods, and properties.
D) None of the above.

View Answer / Hide Answer

ANSWER: A




Q.4 Which of the following statements is correct about an interface used in C#.NET?

A) Interfaces contain no implementation of methods.
B) An interface cannot be instantiated directly.
C) Both A and B option are correct.
D) None of the above.

View Answer / Hide Answer

ANSWER: C




Q.5 According to the given statement, choose the correct option about an interface used in C#.NET.

Statement 1: A class can implement multiple interfaces.
Statement 2: Structures cannot inherit a class but can implement an interface.
Statement 3: An interface can implement multiple classes.
Statement 4: A class can implement only one interface.

A) Only statement 1 is correct.
B) Statement 1 and statement 2 are correct.
C) Statement 3 and statement 4 are correct.
D) Only statement 4 is correct.

View Answer / Hide Answer

ANSWER: B




Q.6 Which of the following is the correct implementation of the interface given below used in C#.NET?

interface IDemo
{
void myFunction(int x);
}

A) class SampleClass : IDemo
{
public void myFunction()
{
// some code
}
}

B) class SampleClass implements IDemo
{
public int myFunction(int x)
{
// some code
}
}

C) class SampleClass : IDemo
{
public void myFunction(int x)
{
// some code
}
}

D) None of the above.

View Answer / Hide Answer

ANSWER: C




Q.7 Which of the following statements is correct?

A) The methods declared in interface are by default sealed.
B) The methods declared in interface are by default public and abstract.
C) The methods declared in interface are by default private.
D) None of the above.

View Answer / Hide Answer

ANSWER: B




Q.8 Choose the correct option about the C#.NET code snippet given below?

interface IMyInterface
{
void function1();
void function2();
}
class MyClass : IMyInterface
{
void function1()
{
// Some code
}
}

A) Run time exception
B) Console.WriteLine("Welcome at CareerRide.com");
C) It will give compile tile error as: 'MyClass' does not implement interface member IMyInterface.function2().
D) None of the above.

View Answer / Hide Answer

ANSWER: C




Q.9 Which of the following is the correct way to implement the interface given below in C#.Net?

interface IEmployee
{
String FirstName
{
get;
set;
}
}
A) class Emp : IEmployee
{
String str;
public string FirstName
{
get{ return str; }
set{ str = value;}
}
}
B) class Emp : implements IEmployee
{
String str;
public string FirstName()
{
get{ return str; }
set{ str = value;}
}
}
C) class Emp : IEmployee
{
String str;
public int FirstName
{
get{ return str; }
set{ str = value;}
}
}

D) None of the above.

View Answer / Hide Answer

ANSWER: A




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

interface IMYInterface
{
//---------
//---------
}
class BaseClass
{
//---------
//---------
}
class DerivedClass :IMYInterface , BaseClass
{

}

A) No problem in the code
B) It gives the compile time error as: Base class 'MyClass' must come before any interfaces.
C) A class cannot implement an interface and another class simultaneously.
D) None of the above

View Answer / Hide Answer

ANSWER: B




Q.11 In the given below code run method is declared in both the interfaces, which is the correct code to call the both run method separately in C#.Net?

interface IBike
{
void run();
}
interface ICar
{
void run();
}

A) class MyClass :IBike,ICar
{
void IBike . run()
{
Console.WriteLine("My bike is running");
}
void ICar . run()
{
Console.WriteLine("My car is running");
}
}

B) class MyClass :IBike,ICar
{
void run()
{
Console.WriteLine("My bike is running");
}
void run()
{
Console.WriteLine("My car is running");
}
}

C) ) class MyClass :IBike
{
void run()
{
Console.WriteLine("My bike is running");
Console.WriteLine("My car is running");
}

}

D) None of the above.

View Answer / Hide Answer

ANSWER: A




Q.12 Refer question 11, choose the correct code to call both run method separately.

A) MyClass obj = new MyClass();
obj.run();
obj.run();

B) MyClass obj = new MyClass();
IBike bike = (IBike)obj;
ICar car = (ICar)obj;
bike.run();
car.run();

C) MyClass obj = new MyClass();
IBike bike = (IBike)obj;
ICar car = (ICar)obj;
IBike.run();
ICar.run();

D) None of the above.

View Answer / Hide Answer

ANSWER: B


Post your comment