1. What happens when you compile and run the below program?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
namespace InterviewNamespace
{
delegate void A(ref string str);
public class sample
{
public static void DelegatePuzzle(ref string a)
{
a = a.Substring(5, a.Length - 5);
}
}
public class Program
{
public static void Main(string[] args)
{
A str1;
string str = "Welcome to CsharpStar !!";
str1 = sample.DelegatePuzzle;
str1(ref str);
Console.WriteLine(str);
}
}
}
|
Output:
me to CsharpStar !!
2. Predict the output for below program.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
public class Program
{
public static void Main(string[] args)
{
#if (DEBUG && !MYTEST)
Console.WriteLine("DEBUG is defined");
#elif (!DEBUG && MYTEST)
Console.WriteLine("MYTEST is defined");
#elif (DEBUG && MYTEST)
Console.WriteLine("DEBUG and MYTEST are defined");
#else
Console.WriteLine("DEBUG and MYTEST are not defined");
#endif
Console.ReadLine();
}
}
|
Output:
DEBUG is defined
3. What will be the output for below program?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
public class Program
{
public static void Main(string[] args)
{
int a = 4;
int c = 2;
bool b = (a % c == 0 ? true : false);
Console.WriteLine(b.ToString());
if (a / c == 2)
{
Console.WriteLine("true");
}
else
{
Console.WriteLine("false");
}
Console.ReadLine();
}
}
|
Output:
True
true
4. Predict the output for below Program
|
public class Program
{
public static void Main(string[] args)
{
bool a = true;
bool b = false;
a |= b;
Console.WriteLine(a);
Console.ReadLine();
}
}
|
Output:
True
5. What will be the output for below program?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
public class Program
{
public static void Main(string[] args)
{
{
try
{
throw new NullReferenceException("C");
Console.WriteLine("A");
}
catch (ArithmeticException e)
{
Console.WriteLine("B");
}
Console.ReadLine();
}
}
}
|
Output:
System.NullReferenceException:C
6. Predict the output for below Program.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
namespace InterviewNamespace
{
class list
{
ArrayList array = new ArrayList();
public object this[int index]
{
get
{
if (index < 0 || index >= array.Count)
{
return null;
}
else
{
return (array[index]);
}
}
set
{
array[index] = value;
}
}
public int Count
{
get;
set;
}
}
class Program
{
static void Main(string[] args)
{
list list1 = new list();
list1[0] = "123";
list1[1] = " abc ";
list1[2] = "xyz";
for (int i = 0; i <= list1.Count; i++)
Console.WriteLine(list1[i]);
Console.ReadLine();
}
}
}
|
Output:
Compiletime Error.Index out of range which arises only when index is non negative or less than the collection of size
7. What will be the output for below program?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
namespace InterviewNamespace
{
class access
{
public int x;
private int y;
public void cal(int a, int b)
{
x = a + 1;
y = b;
}
}
public class Program
{
public static void Main(string[] args)
{
access obj = new access();
obj.cal(2, 3);
Console.WriteLine(obj.x + " " + obj.y);
}
}
}
|
Output:
Compile time error.‘y’ is defined privately which cannot be accessed outside its scope.
8. Predict the Output for below program
|
public class Program
{
public static void Main(string[] args)
{
int x = 100;
int i = 100;
x += i;
x = x + i;
}
}
|
Output:
Compiled successfully
9. What will be the output for below program?
1
2
3
4
5
6
7
8
9
10
11
12
|
class Program
{
static void Main(string[] args)
{
char A = 'G';
char B = Convert.ToChar(76);
A++;
B++;
Console.WriteLine(A + " " + B);
Console.ReadLine();
}
}
|
Output:
H M
10. Predict the output for below program?
1
2
3
4
5
6
7
8
9
10
11
12
13
|
class Program
{
static void Main(string[] args)
{
String s1 = "Welcome to Csharpstar";
String s2 = "welcome to topjavatutorial";
String s3 = s2;
Console.WriteLine((s1 == s2) + " " + s2.Equals(s3));
Console.ReadLine();
}
}
}
|
Output:
false True
0 Comments