1.
What will be the output of the C#.NET code snippet given below?int a = 10, b = 20, c = 30;
int res = a < b ? a < c ? c : a : b;
Console.WriteLine(res);
Correct Answer
A. 30
Explanation
The code snippet uses the ternary operator to compare the values of variables a, b, and c. It first checks if a is less than b, if true, it then checks if a is less than c. If both conditions are true, it assigns the value of c to the variable res. Since a is less than b and c, the value of res will be 30.
2.
Which of the following are the correct ways to declare a delegate for calling the function func() defined in the sample class given below? class Sample
{
public int func(int i, Single j)
{
/* Add code here. */
}
}
Correct Answer
C. Delegate int d(int i, Single j);
Explanation
The correct answer is "delegate int d(int i, Single j)". This is the correct way to declare a delegate for calling the function func() defined in the Sample class. The delegate declaration starts with the keyword "delegate" followed by the return type of the function, which in this case is "int". Then, the delegate name "d" is specified, along with the parameters of the function, which are "int i" and "Single j".
3.
A derived class can stop virtual inheritance by declaring an override as
Correct Answer
C. Sealed
Explanation
In object-oriented programming, when a class is declared as "sealed", it means that it cannot be inherited by any other class. In the context of the given question, if a derived class declares a method as "sealed", it indicates that this method cannot be overridden by any further derived classes. Therefore, the correct answer is "sealed" as it stops the virtual inheritance by preventing further overriding of the method in derived classes.
4.
For the code snippet shown below, which of the following statements are valid? public class Generic<T>
{
public T Field;
public void TestSub()
{
T i = Field + 1;
}
}
class MyProgram
{
static void Main(string[] args)
{
Generic<int> gen = new Generic<int>();
gen.TestSub();
}
}
Correct Answer
B. Compiler will report an error: Operator '+' is not defined for types T and int.
Explanation
The code snippet defines a generic class Generic with a public field of type T and a method TestSub() which tries to perform addition on the field. In the Main method, an instance of Generic is created and the TestSub() method is called. Since the type T is not constrained to be a type that supports addition, the compiler will report an error stating that the operator '+' is not defined for types T and int.
5.
For the code snippet shown below, which of the following statements are valid?
public class TestIndiaBix
{
public void TestSub (M arg)
{
Console.Write(arg);
}
}
class MyProgram
{
static void Main(string[] args)
{
TestIndiaBix bix = new TestIndiaBix();
bix.TestSub("IndonesiaBIX ");
bix.TestSub(4.2f);
}
}
Correct Answer
B. Compiler will generate an error.
Explanation
The TestSub method in the TestIndiaBix class is expecting a parameter of type M, but no definition of type M is provided in the given code snippet.
When calling the TestSub method with arguments "IndonesiaBIX " and 4.2f, the compiler will not be able to find a suitable overload of the TestSub method that accepts arguments of type string and float, respectively.
Therefore, the compiler will generate an error indicating that there is no suitable method overload for the provided arguments.
6.
Which of the following statements are correct about an ArrayList collection that implements the IEnumerable interface?- The ArrayList class contains an inner class that implements the IEnumeratorinterface.
- An ArrayList Collection cannot be accessed simultaneously by different threads.
- The inner class of ArrayList can access ArrayList class's members.
- To access members of ArrayList from the inner class, it is necessary to passArrayList class's reference to it.
- Enumerator's of ArrayList Collection can manipulate the array.
Correct Answer
C. 1 and 3 and 4 only
Explanation
The given correct answer is 1 and 3 and 4 only. This means that the statements 1, 3, and 4 are correct about an ArrayList collection that implements the IEnumerable interface. Statement 1 is correct because the ArrayList class contains an inner class that implements the IEnumerator interface. Statement 3 is correct because the inner class of ArrayList can access ArrayList class's members. Statement 4 is correct because to access members of ArrayList from the inner class, it is necessary to pass ArrayList class's reference to it.
7.
In which of the following collections is the Input/Output index-based? - Stack
- Queue
- BitArray
- ArrayList
- HashTable
Correct Answer
C. 3 and 4 only
Explanation
The correct answer is 3 and 4 only. A BitArray is a collection that allows access to its elements using an index, similar to an array. A HashTable, on the other hand, is not index-based but instead uses keys to access its elements. The other options, Stack, Queue, and ArrayList, also do not support index-based access.
8.
Which of the following statements is correct about the C#.NET code snippet given below? int a = 10;
int b = 20;
int c = 30;
enum color: byte
{
red = a,
green = b,
blue = c
}
Correct Answer
B. Variables cannot be assigned to enum elements.
Explanation
The given C#.NET code snippet is attempting to assign variables a, b, and c to the enum elements red, green, and blue respectively. However, this is not allowed in C#.NET. Enum elements must be assigned constant values at compile-time, and variables are not considered constant values. Therefore, the correct statement is that variables cannot be assigned to enum elements.
9.
Which of the following statements is correct about the C#.NET code snippet given below? switch (id)
{
case 6:
grp = "Grp B";
break;
case 13:
grp = "Grp D";
break;
case 1:
grp = "Grp A";
break;
case ls > 20:
grp = "Grp E";
break ;
case Else:
grp = "Grp F";
break;
}
Correct Answer
B. Compiler will report an error in case ls > 20 as well as in case Else.
Explanation
The given C#.NET code snippet contains an error in the switch case statement. The cases "case ls > 20" and "case Else" are not valid. The switch statement only accepts constant values as cases, not expressions or conditions. Therefore, the compiler will report an error in both of these cases.
10.
What will be the output of the C#.NET code snippet given below? int i = 2, j = i;
if (Convert.ToBoolean((i | j & 5) & (j - 25 * 1)))
Console.WriteLine(1);
else
Console.WriteLine(0);
Correct Answer
B. 0
Explanation
The code snippet initializes two variables, i and j, with the value of 2. It then evaluates the condition inside the if statement. The condition is a combination of bitwise OR, bitwise AND, and arithmetic operations.
First, the bitwise OR operation is performed on i and j with the number 5. This results in 7.
Next, the bitwise AND operation is performed on the previous result and the subtraction of j from 25 multiplied by 1. Since j is equal to i, which is 2, the subtraction is 0. Therefore, the bitwise AND operation results in 0.
Finally, the Convert.ToBoolean() method is called on the previous result, which is 0. Since 0 is considered as false, the else block is executed and the output is 0.
11.
Classes and ObjectsWhich of the following will be the correct output for the C#.NET program given below?namespace IndiabixConsoleApplication
{
class Sample
{
int i;
Single j;
public void SetData(int i, Single j)
{
this.i = i;
this.j = j;
}
public void Display()
{
Console.WriteLine(i + " " + j);
}
}
class MyProgram
{
static void Main(string[ ] args)
{
Sample s1 = new Sample();
s1.SetData(36, 5.4f);
s1.Display();
}
}
}
Correct Answer
A. 36 5.4
Explanation
The correct output for the given C#.NET program will be "36 5.4". This is because the SetData method of the Sample class is called with the arguments 36 and 5.4f, which sets the values of the variables i and j respectively. The Display method then prints the values of i and j, resulting in the output "36 5.4".
12.
Array. Which of the following is the correct output of the C#.NET code snippet given below? int[ , , ] a = new int[ 3, 2, 3 ];
Console.WriteLine(a.Length);
Correct Answer
B. 18
Explanation
The code snippet creates a three-dimensional array with dimensions 3, 2, and 3. The Length property returns the total number of elements in the array, which is calculated by multiplying the lengths of all dimensions together. In this case, the length of the first dimension is 3, the length of the second dimension is 2, and the length of the third dimension is 3. Therefore, the total number of elements in the array is 3 * 2 * 3 = 18.
13.
Which of the following statements are correct about a namespace used in C#.NET?- Classes must belong to a namespace, whereas structures need not.
- Every class, struct, enum, delegate and interlace has to belong to some or the other namespace.
- All elements of the namespace have to belong to one file.
- If not mentioned, a namespace takes the name of the current project.
- The namespace should be imported to be able to use the elements in it.
Correct Answer
A. 2, 4, 5
Explanation
The given answer is correct because in C#.NET, classes must belong to a namespace, whereas structures do not necessarily need to belong to a namespace. Additionally, if a namespace is not mentioned, it takes the name of the current project. Finally, a namespace needs to be imported in order to use the elements within it.
14.
If ListBox is class present in System.Windows.Forms namespace, then which of the following statements are the correct way to create an object of ListBox Class? - using System.Windows.Forms;
ListBox lb = new ListBox();
- using LBControl = System.Windows.Forms;
LBControl lb = new LBControl();
- System.Windows.Forms.ListBox lb = new System.Windows.Forms.ListBox();
- using LBControl lb = new System.Windows.Forms.ListBox;
- using LBControl = System.Windows.Forms.ListBox;
LBControl lb = new LBControl();
Correct Answer
B. 1, 3, 5
Explanation
The correct way to create an object of ListBox class is by using the following statements:
1. ListBox lb = new ListBox();
3. System.Windows.Forms.ListBox lb = new System.Windows.Forms.ListBox();
5. LBControl lb = new LBControl();
These statements correctly import the necessary namespace (System.Windows.Forms) and create an object of the ListBox class using the appropriate syntax.
15.
Which of the following statements is correct about the C#.NET code snippet given below?
class Trial
{
int i;
Decimal d;
}
struct Sample
{
private int x;
private Single y;
private Trial z;
}
Sample ss = new Sample();
Correct Answer
D. Trial object referred by z will be created on the stack.
Explanation
Trial is a class, and instances of classes are typically created on the heap. However, when a class object is a member of a struct, as in the case of Trial z; within the Sample struct, the memory for that object is part of the struct's memory allocation.
When an instance of the Sample struct is created (Sample ss = new Sample();), it will be allocated on the stack.
The Trial object referred to by z will be part of the Sample struct on the stack, and its memory will be managed along with the struct.
Therefore, the Trial object referred to by z is created on the stack, while the entire Sample struct (ss) is created on the stack.
16.
Which of the following will be the correct result of the statement b = a in the C#.NET code snippet given below?
struct Address
{
private int plotno;
private String city;
}
Address a = new Address();
Address b;
b = a;
Correct Answer
C. All elements of a will get copied into corresponding elements of b.
Explanation
When a struct is assigned to another struct variable in C#, a member-wise copy of the values of all fields in 'a' is made into 'b'. Therefore, all elements (fields) of struct 'a' will be copied into corresponding elements of struct 'b', resulting in 'b' containing the same values as 'a'. This process does not involve garbage collection or scoping as structs are value types and do not rely on memory allocation on the heap like reference types.
17.
What will be the output of the C#.NET code snippet given below?namespace IndiabixConsoleApplication
{
class Baseclass
{
public void fun()
{
Console.Write("Base class" + " ");
}
}
class Derived1: Baseclass
{
new void fun()
{
Console.Write("Derived1 class" + " ");
}
}
class Derived2: Derived1
{
new void fun()
{
Console.Write("Derived2 class" + " ");
}
}
class Program
{
public static void Main(string[ ] args)
{
Derived2 d = new Derived2();
d.fun();
}
}
}
Correct Answer
C. Derived1 class
Explanation
The output of the code will be "Derived1 class". This is because the Main method creates an instance of the Derived2 class and calls the fun() method on that instance. However, the fun() method in the Derived2 class is marked with the "new" keyword, which hides the fun() method in the Baseclass. Since the instance is of type Derived2, the fun() method in the Derived2 class is called, resulting in the output "Derived1 class".
18.
In an inheritance chain which of the following members of base class are accessible to the derived class members?
- static
- protected
- private
- shared
- public
Correct Answer
A. 2, 5
Explanation
In an inheritance chain, the derived class can access the static and public members of the base class. Therefore, the correct answer is 2 and 5.
19.
Which of the following statements are correct?- An argument passed to a ref parameter need not be initialized first.
- Variables passed as out arguments need to be initialized prior to being passed.
- Argument that uses params keyword must be the last argument of variable argument list of a method.
- Pass by reference eliminates the overhead of copying large data items.
- To use a ref parameter only the calling method must explicitly use the refkeyword.
Correct Answer
A. 3, 4
Explanation
The third statement is correct because an argument that uses the params keyword must be the last argument of a variable argument list of a method. The fourth statement is correct because pass by reference eliminates the overhead of copying large data items.
20.
Which of the following is the correct ways to set a value 3.14 in a variable pi such that it cannot be modified?
Correct Answer
A. Const float pi = 3.14F;
Explanation
The correct answer is "const float pi = 3.14F;". This is because using the "const" keyword before declaring a variable makes it a constant, meaning its value cannot be modified once it is assigned. In this case, the variable "pi" is declared as a constant float with a value of 3.14F, ensuring that its value cannot be changed.
21.
Which of the following keyword is used to overload user-defined types by defining static member functions?
Correct Answer
D. Operator
Explanation
The keyword "operator" is used to overload user-defined types by defining static member functions. Overloading allows us to define multiple functions with the same name but different parameters, and it can be used to redefine the behavior of an operator when applied to user-defined types. By defining a static member function with the keyword "operator", we can specify how an operator should behave when used with a specific user-defined type.
22.
A Student class has a property called rollNo and stu is a reference to a Studentobject and we want the statement stu.RollNo = 28 to fail. Which of the following options will ensure this functionality?
Correct Answer
B. Declare rollNo property with only get accessor.
Explanation
If we declare the rollNo property with only a get accessor, it means that we can only retrieve the value of the rollNo property but cannot set a new value to it. Therefore, the statement stu.RollNo = 28 will fail because we are trying to assign a new value to the rollNo property, but it is not allowed due to the absence of a set accessor.
23.
Which of the following will be the correct output for the C#.NET code snippet given below?String s1 = "Five Star";
String s2 = "FIVE STAR";
int c;
c = s1.CompareTo(s2);
Console.WriteLine(c);
Correct Answer
B. -1
Explanation
The code snippet compares two strings, "Five Star" and "FIVE STAR", using the CompareTo method. The CompareTo method returns an integer value that indicates the lexicographic relationship between the two strings. In this case, since "Five Star" comes before "FIVE STAR" in alphabetical order, the CompareTo method will return a negative value. Therefore, the correct output for this code will be -1.
24.
Which of the following ways to create an object of the Sample class given below will work correctly?
class Sample
{
int i;
Single j;
double k;
public Sample (int ii, Single jj, double kk)
{
i = ii;
j = jj;
k = kk;
}
}
Correct Answer
B. Sample s3 = new Sample(10, 1.2f, 2.4);
Explanation
The Sample class has a constructor that takes three parameters: an int, a Single, and a double. When creating an object of the Sample class, we need to provide values for all three parameters. Therefore, the correct way to create the object is by passing values for all three parameters in the constructor: new Sample(10, 1.2f, 2.4);.
25.
Which of the following is the root of the .NET type hierarchy?
Correct Answer
B. System.Object
Explanation
The root of the .NET type hierarchy is System.Object. In .NET, all types inherit from the System.Object class, which is the base class for all other classes. This means that every class in .NET is a descendant of System.Object and inherits its methods and properties.