C# Question - Answer
C# Question - Answer
namespace Arrays_1
{
class Program
{
static void Main(string[] args)
{
int i;
if ((i = 10 / 2) = 0)
Console.WriteLine("Even Number");
else
Console.WriteLine("Odd Number");
Console.Read();
}
}
}
Options
Question Text 2.
Consider the following code snippet:
using System;
class Break
{
}
Console.WriteLine(ch);
break;
}
}
Console.ReadLine();
}
}
Options
Question Text 3.
namespace Arrays_1
{
class Program
{
static void Main(string[] args)
{
int[] arrayFirst = { 10, 20, 30, 40, 50 };
int[] arraySecond= {1 , 2, 3, 4, 5};
int[] arraythird= new int[5];
int counter = 0; //Line 1
for (counter = 0; counter < 5; counter++) //Line 2
{
arraythird[counter] = arrayFirst[counter] + array[counter]; //Line 3
}
for (counter = 0; counter < 5; counter++) //Line 4
Console.WriteLine("{0}", arraythird[counter]);
Console.ReadLine();
}
}
}
Identify the error
Options
1. Line 1
2. Line 2
3. Line 3
4. Line 4
Question Text 4.
Options
Question Text 5.
}
}
}
Options
1. Line 1
2. Line 2
3. Line 3
4. Line 4
Question Text 6.
namespace Const2
{
class Program
{
public int count;
public Program()
{
count = 1;
}
public void inc_count()
{
count++;
}
public int get_count()
{
return count;
}
}
class count_class
{
static void Main(string[] args)
{
Program P1 = new Program();
Program P2 = new Program();
Console.WriteLine("P1= {0}",P1.get_count ());
P1.inc_count();
P2.inc_count();
Console.WriteLine("P2= {0}",P2.get_count ());
Console.ReadLine();
}
}
}
Options
1. P1= 1
P2= 2
2. P1=1
P2=1
3. P1=2
P2=2
4. Compilation error
Question Text 7.
Console.WriteLine("Enter the price within which you want to buy the watch");
Watch_Price=Convert.ToInt32(Console.ReadLine());
}
}
The preceding code when executed shows the error. Identify the error in the preceding code.
Options
Question Text 8.
Options
Question Text 9.
Options
Options
*************************************************************************************
using System;
class AddNumbers
{
class Calculator
{
void AddOne(int var)
{
var++;
++var;
var++;
}
public static void Main()
{
Calculator obj = new Calculator();
int number = 6;
obj.AddOne(number);
Console.WriteLine(number);
Console.Read();
}
}
}
Options
1. 6
2. 8
3. 9
4. 7
Identify the component of the .NET Framework which allows the execution of code across different
platforms by translating code into Intermediate Language.
Options
namespace Arrays_1
{
class Program
{
static void Main(string[] args)
{
int[] arrayFirst = { 10, 20, 30, 40, 50 };
int[] arraySecond= {1, 2, 3, 4, 5};
int[] arraythird= new int[5];
int counter = 0;
for (counter = 0; counter < 5; counter++)
{
arraythird[counter] = arrayFirst[counter] +
arraySecond[counter];
}
for (counter = 0; counter < 5; counter++)
Console.WriteLine("{0}", arraythird[counter]);
Console.ReadLine();
}
}
}
Predict the output of the preceding code.
Options
1. 11
22
33
44
55
2. 10
20
30
40
50
3. 1
2
3
4
5
4. Compilation error
namespace Arrays_1
{
class Program
{
static void Main(string[] args)
{
double[] nums; //Line 1
double result = 0.0; //Line 2
int i;
for (i = 0; i < 5; i++) //Line 3
Options
1. Line 4
2. Line 2
3. Line 3
4. Line 1
Options
1. It will print
10
20
2. It will print
10
3. It will print
20
10
4. Compilation error
Correct Answer: -> 1
class Base
{
~Base()
{
Console.WriteLine("Destroying Base");
}
}
class Derived : Base
{
~Derived()
{
Console.WriteLine("Destroying Derived");
}
}
class Test
{
static int Main(string[] args)
{
Base obj = new Derived();
Console.Read();
return 0;
}
}
Options
1. It will print:
Destroying Base
Destroying Derived
2. It will print:
Destroying Derived
Destroying Base
3. It will print:
Destroying Base
4. It will print:
Destroying Derived
class AddNumbers
{
int result;
AddNumbers()
{
result = 0;
}
public void Divide(int number1, int number2)
{
try
{
result = number1 / number2;
}
catch (DivideByZeroException e)
{
Console.WriteLine("Exception caught.{0} ", e);
}
finally
{
Console.WriteLine("Result is {0}", result);
}
}
public static void Main(string[] args)
{
AddNumbers Division = new AddNumbers();
Division.Divide(10, 5);
Console.ReadLine();
}
}
Options
Options
1. Syntax error
2. Run-Time error
3. Logical error
4. Zero division error
class Test
{
static int Main(string[] args)
{
Derived obj = new BaseDerived();
Console.Read();
return 0;
}
}
Options
1. Constructor of Base
Constructor of Derived
Constructor of BaseDerived
2. Constructor of BaseDerived
Constructor of Derived
Constructor of Base
3. Constructor of Base
Constructor of BaseDerived
Constructor of Derived
4. Constructor of BaseDerived
Constructor of Base
Constructor of Derived
*************************************************************************************
Question Text 20.
struct NumberCount
{
public int i;
public NumberCount(int initval)
{
this.i = initval;
}
public static NumberCount operator ++(NumberCount arg)
{
arg.i++;
return arg;
}
}
class TestClass
{
static void Main(string[] args)
{
Console.WriteLine(Count1.i);
Console.WriteLine(Count2.i);
Console.ReadLine();
}
}
Options
1. It will print:
1
2
3
3
2. It will print:
3
3
1
2
3. It will print:
2
1
3
3
4. It will print:
2
1
3
2
class Program
{
String name;
int age;
public Program(int i)
{
age = i;
}
static void Main(string[] args)
{
Program p = new Program();
Console.Write("Enter name: ");
p.age=Convert.ToInt16(Console.ReadLine());
Console.WriteLine("Name: {0}",p.name);
Console.WriteLine("Age: {0}", p.age);
}
}
Options
1. It will print the name and age entered by the user in the same line.
2. It will print the name and age entered by the user in the different lines.
3. It will print the name as entered by the user but the age will be printed as 0.
4. It will give an error.
Analyse the following code and choose the most appropriate option.
using System;
using System.Threading;
class AddNumbers
{
Thread1.Priority = ThreadPriority.Lowest ;
Thread2.Priority = ThreadPriority.Highest ;
Thread1.Start();
Thread2.Start();
Console.Read();
}
Options
Analyze the following code and select the most appropriate option.
Options
Sam has created the following program to read the contents of a file. During the execution of the
program he found that only a single character is repeatedly displayed on the console and the program
runs into an infinite loop. Find the possible cause of the error.
using System;
using System.IO;
class Program
{
static int Main(string[] args)
{
char ch;
FileStream fs = new FileStream(@"c:\preeti\myfile.txt", FileMode.Open , FileAccess.Read );
StreamReader sr = new StreamReader(fs);
ch=Convert.ToChar(sr.Read());
while (ch != -1)
Console.Write("{0}",ch);
Console.Read();
return 0;
}
}
Options
using System;
using System.Timers;
class myApp
{
public static void Main()
{
Timer myTimer = new Timer();
myTimer.Elapsed += new ElapsedEventHandler( DisplayTimeEvent );
myTimer.Interval = 1000;
myTimer.Start();
Options
A programmer is developing software for a recruitment company. He is following the OOPS concept. He
has created a general class named Candidate. He is using this class as a super class only. He is not
creating any object of this class.
Which type of class is the candidate class?
Options
1. Subclass
2. Super Class
3. Class
4. Abstract Class
using System;
public class MyClass
{
class testing
{
public int Division(int a, int b)
{
return a/b;
}
public float Division(int a, int b)
{
float x,y;
x=a;y=b;
return x / y;
}
Options
1. It will print
5
2.5
2. It will print
2.5
5
3. It will print
5 2.5
4. It will give an error.
Analyze the following program and choose the most appropriate answer.
class AddNumbers
{
public static void Adding_Numbers(int number1, int number2)
{
try
{
int res = number1 / number2;
Console.WriteLine(res);
}
catch (DivideByZeroException e)
{
Console.WriteLine("Exception caught. {0} ", e);
}
}
public static void Main()
{
AddNumbers.Adding_Numbers(10, 0);
Console.ReadLine();
}
}
Options
1. Symantec Error
2. Syntax error
3. Logical error
4. Run-Time error
Correct Answer: -> 4
Sam is destroying a thread in his program by using the Thread.Abort() method. Identify the mechanism
used by the CLR to detroy the thread when the Thread.Abort() method is used.
Options
1. The CLR destroys a thread by calling Sleep method for infinite time.
2. The CLR destroys a thread by throwing an exception.
3. The CLR destroys a thread by calling the Suspend method.
4. The CLR destroys a thread by calling the Resume method.
A programmer is using the StreamReader class in his program. He needs a method, which returns the
next available character but does not consume it. Identify the method, which could be used for the
purpose.
Options
1. Close
2. Read
3. Peek
4. Seek
A programmer needs to make a thread wait for some specified condition to be satisfied by calling the
Wait() method. Which method will be used to notify the thread about the condition?
Options
1. Pulse() method
2. Start() method
3. Resume() method
4. Suspend() method
Options
ChildThread.Abort();
ChildThread.Start();
Console.ReadLine();
}
}
Options
To use a variable num for storing a numeric value without any fractional part, the variable num must be
declared and it should be of the type:
Options
1. char
2. numeric
3. float
4. positive
Options
1. Statements
2. Functions
3. Structured programs
4. Variables
Options
1. will give the output "hello".
2. will give error.
3. will read a string "hello".
4. compile successfully but no output is displayed.
using System;
class demo
{
public static void Main(string[]args)
{
bool compare;
string s1,s2;
s1 = "Welcome";
s2 = "NIITians";
compare=(s1=="Hello")^(s2=="NIITians");
Console.WriteLine(compare.ToString());
}
}
Options
1. The message False will be displayed because both the expression are true.
2. The message True will be displayed because both the expressions are false.
3. The message False will be displayed because both the expressions are false.
4. The message True will be displayed because one expression is false.
Options
1. Console.WriteLine("{0}",a);
while(b < 100)
{
Console.WriteLine( b);
b=b+a;
a=b-a;
}
2. Console.WriteLine("{0}",a);
while(b<100)
{
Console.WriteLine(b);
b = b++;
a = a-b;
}
3. Console.WriteLine("{0}",a);
while(b>100)
{
Console.WriteLine( b);
b=b+a;
a=b-a;
}
4. Console.WriteLine("{0}",a);
while(b<100)
{
Console.WriteLine( b);
b=b+a;
a=a--;
}
1. < return type > < access specifier > < method name > (parameter list)
{
method body
}
2. < return type > < access specifier > < method name > [parameter list]
{
method body
}
3. < access specifier > < return type > < method name > { parameter list}
{
Method body
}
4. < access specifier > < return type > < method name > (parameter list)
{
Method body
}
Options
Options
1. class b:a
{
public override void show()
{
Console.WriteLine("you are in class b");
}
}
2. class b:a
{
public void show()
{
a:s();
Console.WriteLine("you are in class b");
}
3. class b
{
public void show()
{
a obj = new a();
obj.show();
}
4. class b:a
{
private override void show()
{
Console.WriteLine("you are in class b");
}
Options
1. class emp
{
{
string name;
int eid;
void accept_details[]
{}
void display_details()
{}
}
}
2. class emp
{
{
void accept_details()
{
string name;
int eid;
}
void display-details()
{
string name;
int eid;
}
}
}
3. class emp
{
string e_name;
int e_id;
void accept_details()
{}
void display_details()
{}
}
4. class emp
{
void accept_details()
{
string name;
int eid;
}
void display_details()
{
string name;
int eid;
}
}
Options
1. Dec=12
2. Dec = 4
3. Dec = 3
4. Dec = 0
class over_demo
{
static void Main(string[] args)
{
emp e1=new emp(2);
emp e2 = --e1;
Console.WriteLine(e1.id);
Console.WriteLine(e2.id);
e2=e1--;
Console.WriteLine(e1.id);
Console.WriteLine(e1.id);
Console.ReadLine();
}
}
}
Options
1. 1
2
0
0
2. 1
1
0
0
3. 2
1
0
0
4. 2
2
0
0
using System;
namespace s
{
class enum_demo
{
enum e{jan,feb,mar,dec};
static void Main(string[] args)
{
string m1=(string)e.dec;
Console.WriteLine("{0}",m1);
Console.ReadLine();
}
}
}
Options
1. Dec
2. 3
3. It will display the message, Can't convert type 's.enum_demo.e' to string.
4. The program will compile but no message will be displayed.
Options
Choose the correct code to define the event named 'ehello' that invokes a delegate named 'hello'
Options
Options
1. Static constructors are used only by static classes to initialize their variables.
2. Static constructors are used for initializing both static and non-static variables of a class.
3. The static constructors have an implicit public access.
4. The static constructors have an implicit private access.
Options
1. It will display:
destructor can not be defined before constructor
2. It will display:
you are in destructor
3. It will display:
you are in constructor
4. It will give compilation error.
The following code was written to write in a file named "abc.txt". The code is not performing the desired
task. Identify the code snippet that will help perform the desired task.
using System;
using System.IO;
class file_demo
{
public void accept()
{
FileStream f = new FileStream("abc.txt");
StreamWriter s = new StreamWriter(f);
Console.WriteLine("Enter the String:");
string s1 = Console.ReadLine();
s.Write(s1);
s.Flush();
s.Close();
f.Close();
}
public static void Main(string[] args)
{
file_demo obj = new file_demo();
obj.accept();
}
}
Options
Options
1. It will call the unmanaged code defined in kernel from managed C# environment.
2. It will call the unmanaged code defined in Dll from managed C# environment.
3. It will call and import the unmanaged code residing in kernel to Dll.
4. It will import the unmanaged code residing in Dll to kernel.
Options
1. DeadLock
2. Race condition
3. Lock starvation
4. Wait
Consider the following code that is written to abort a thread but is not able to abort the child
thread,why? Give reason.
using System;
using System.Threading;
namespace ThreadSample
{
class Thred_demo1
{
public static void Child()
{
try
{
Console.WriteLine("Child thread started");
Console.WriteLine
("Child thread - counting to 10");
for (int i = 0; i < 10; i++)
{
Thread.Sleep(500);
Console.Write("{0}...", i);
}
Console.WriteLine("Child thread finished");
}
catch (ThreadAbortException e)
{
Console.WriteLine("Exception"+e);
}
finally
{
Console.WriteLine
("Child thread -Unable to catch the exception.");
}
}
public static void Main()
{
ThreadStart ChildRef = new ThreadStart(Child);
Console.WriteLine("Main - Creating Child thread");
Thread ChildThread = new Thread(ChildRef);
ChildThread.Start();
Console.WriteLine("Main - Sleeping for 2 seconds");
Thread.Sleep(2000);
Console.WriteLine("\nMain - Aborting Child thread");
Console.ReadLine();
}
}
}
Options