Thursday, July 24, 2014

C# Practice Tests

#  Test / Quiz / Q&A / Online Quiz / Online Test / Interview Questions /  C# Question Answers / C#.NET Basic / C# Fundamentals / C# Learning
1. Which of the following statements are true about the C#.NET code snippet given below?
      
String s1, s2;
       s1 = "Hi";
       s2 = "Hi";
   1)  String objects cannot be created without using new.
   2)  Only one object will get created.
   3)  s1 and s2 both will refer to the same object.
   4)  Two objects will get created, one pointed to by s1 and another pointed to by s2.
   5)  s1 and s2 are references to the same object. 


   A. 1, 2, 4
   B. 2, 3, 5
   C. 3, 4
   D. 2, 5


2. Which of the following will be the correct output for the C#.NET code snippet given below?
 
  String s1 = "ALL MEN ARE CREATED EQUAL";
   String s2;
   s2 = s1.Substring(12, 3);
   Console.WriteLine(s2);


    A. ARE
    B. CRE 
    C. CR
    D. REA
    E. CREATED


3. Which of the following statements will correctly copy the contents of one string into another?
   A.   String s1 = "String";
          String s2;
          s2 = s1;            
   B.   String s1 = "String" ;
         String s2;
         s2 = String.Concat(s1, s2);      
   C.   String s1 = "String";
         String s2;
         s2 = String.Copy(s1);
   D.   String s1 = "String";
         String s2;
        s2 = s1.Replace();
   E.   String s1 = "String";
         String s2;
         s2 = s2.StringCopy(s1);


4. The string built using the String class are immutable (unchangeable), whereas, the ones
   built- using the StringBuilder class are mutable
.

      A.    True  B.    False

5. Which of the following will be the correct output for the C#.NET code snippet given below?    String s1 = "Nagpur";
   String s2;
   s2 = s1.Insert(6, "Mumbai");
   Console.WriteLine(s2);

   A. NagpuMumbair
   B. Nagpur Mumbai
   C. Mumbai
   D. Nagpur
   E. NagpurMumbai
6. If s1 and s2 are references to two strings, then which of the following is the correct way to
    compare the two references?
   A. s1 is s2
   B. s1 = s2
   C. s1 == s2
   D. strcmp(s1, s2)
   E. s1.Equals(s2)
7. What will be the output of the C#.NET code snippet given below?
   namespace IndiabixConsoleApplication
    {
      class SampleProgram
       {
         static void Main(string[ ] args)
          {
            string str= "Hello World!";
            Console.WriteLine( String.Compare(str, "Hello World?" ).GetType() );
          }
        }
     }             
    A.0         B. 1         C.String       D. Hello World?    E.System.Int32
8. Which of the following snippets is the correct way to convert a Single into a String?
   1.  Single f = 9.8f;
       String s;
       s = (String) (f);
   2.  Single f = 9.8f;
       String s;
       s = Convert.ToString(f);

   3.  Single f = 9.8f;
       String s;
       s = f.ToString();

   4.  Single f = 9.8f;
       String s;
       s = Clnt(f);

   5. Single f = 9.8f;
      String s;
     s = CString(f);

    A. 1, 2       B. 2, 3   C. 1, 3, 5

9. Which of the following will be the correct output for the C#.NET code snippet given below?
 
   String s1="Kicit";
   Console.Write(s1.IndexOf('c') + " ");
   Console.Write(s1.Length);

   A. 3 6     B. 2 5     C. 3 5     D. 2 6     E.3 7

10. Which of the following is correct way to convert a String to an int?
 
   1.  String s = "123";
       int i;
       i = (int)s;   

   2.  String s = "123";
       int i;
       i = int.Parse(s);  

   3.  String s = "123";
       int i;
       i = Int32.Parse(s); 
  
   4.  String s = "123";
       int i;
       i = Convert.ToInt32(s);   

   5. String s = "123";
      int i;
      i = CInt(s);  

    A. 1, 3, 5         B. 2, 4              C. 3, 5              D. 2, 3, 4

11. Which of the following statements about a String is correct?
    A.  A String is created on the stack.
    B.  Whether a String is created on the stack or the heap depends on the length of the String.
    C.  A String is a primitive.
    D.  A String can be created by using the statement String s1 = new String;
    E.  A String is created on the heap.

12. Which of the following statement is correct about a String in C#.NET?
    A.  A String is mutable because it can be modified once it has been created.
    B.  Methods of the String class can be used to modify the string.
    C.  A number CANNOT be represented in the form of a String.
    D.  A String has a zero-based index.
    E.  The System.Array class is used to represent a string.

13. 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);   

    A. 0        B. 1         C. 2         D. -1       E. -2
 
14. If s1 and s2 are references to two strings then which of the following are the correct ways to find whether the contents of the two strings are equal?
    1.  if(s1 = s2)
    2.  if(s1 == s2)
    3.  int c;
        c = s1.CompareTo(s2);
    4.  if( strcmp(s1, s2) )
    5.  if (s1 is s2)  

    A. 1, 2    B. 2, 3    C. 4, 5    D. 3, 5

15. Which of the following statements are correct about the String Class in C#.NET?
   
    1. Two strings can be concatenated by using an expression of the form s3 = s1 + s2;
    2. String is a primitive in C#.NET.
    3. A string built using StringBuilder Class is Mutable.
    4. A string built using String Class is Immutable.
    5. Two strings can be concatenated by using an expression of the form s3 = s1&s2;

    A. 1, 2, 5               B. 2, 4      C. 1, 3, 4             D. 3, 5

16. Which of the following statements are correct?
    1. String is a value type.
    2. String literals can contain any character literal including escape sequences.
    3. The equality operators are defined to compare the values of string objects as well as references.
    4. Attempting to access a character that is outside the bounds of the string results in an IndexOutOfRangeException.
    5. The contents of a string object can be changed after the object is created.
   
     A. 1, 3    B. 3, 5    C. 2, 4    D.1, 2, 4

17. Which of the following is the correct way to find out the index of the second’s’ in the string "She sells sea shells on the sea-shore"?
   
    A. String str = "She sells sea shells on the sea-shore";
       int i;
       i = str.SecondIndexOf("s");
    B. String str = "She sells sea shells on the sea-shore";
       int i, j;
       i = str.FirstIndexOf("s");
       j = str.IndexOf("s", i + 1);
    C. String str = "She sells sea shells on the sea-shore";
       int i, j;
       i = str.IndexOf("s");
       j = str.IndexOf("s", i + 1);
    D. String str = "She sells sea shells on the sea-shore";
       int i, j;
       i = str.LastIndexOf("s");
       j = str.IndexOf("s", i - 1);
    E. String str = "She sells sea shells on the sea-shore";
       int i, j;
       i = str.IndexOf("S");
       j = str.IndexOf("s", i);

C# Exception Handling Test:
     
1. Which of the following is NOT a .NET Exception class?
   A. Exception
   B. StackMemoryException
   C. DivideByZeroException
   D. OutOfMemoryException
   E. InvalidOperationException
2. Which of the following statements is correct about an Exception?
   A. It occurs during compilation.
   B. It occurs during linking.
   C. It occurs at run-time.
   D. It occurs during Just-In-Time compilation.
   E. It occurs during loading of the program.
3. In C#.NET if we do not catch the exception thrown at runtime then which of the following will catch it?
   A. Compiler
   B. CLR
   C. Linker
   D. Loader
   E. Operating system
4. Which of the following statements is correct about the C#.NET program given below?
   using System;
   namespace IndiabixConsoleApplication
     {
      class MyProgram
       {
         static void Main(string[] args)
          {
            int index = 6;
            int val = 44;
            int[] a = new int[5];
            try
            {
                a[index] = val ;
            }   
            catch(IndexOutOfRangeException e)
            {
                Console.Write("Index out of bounds ");
            }
            Console.Write("Remaining program");
          }
       }
     }
     A. Value 44 will get assigned to a[6].
     B. It will output: Index out of bounds
     C. It will output: Remaining program
     D. It will not produce any output.
     E. It will output: Index out of bounds Remaining program
5. Which of the following statements are correct about exception handling in C#.NET?
   1. If an exception occurs then the program terminates abruptly without getting any chance to recover from the exception.
   2. No matter whether an exception occurs or not, the statements in the finally clause (if present) will get executed.
   3. A program can contain multiple finally clauses.
   4. A finally clause is written outside the try block.
   5. Finally clause is used to perform clean-up operations like closing the network/database connections.
   A. 1 only
   B. 2 only
   C. 2 and 5 only
   D. 3 and 4 only
   E. None of the above
6. Which of the following statements are correct about exception handling in C#.NET?
   1. If our program does not catch an exception then the .NET CLR catches it.
   2. It is possible to create user-defined exceptions.
   3. All types of exceptions can be caught using the Exception class.
   4. CLRExceptions is the base class for all exception classes.
   5. For every try block there must be a corresponding finally block.
   A. 1 and 2 only
   B. 1, 2 and 3 only
   C. 4 and 5 only
   D. All of the above
   E. None of the above
7. Which of the following statements are correct about the exception reported below?
   Unhandled Exception: System.lndexOutOfRangeException: Index was outside the bounds of the array:
   at IndiabixConsoleApplication.MyProgram.SetVal(Int32 index, Int32 val) in
   D:\Sample\IndiabixConsoleApplication\MyProgram.cs:line 26 at
   IndiabixConsoleApplication.MyProgram.Main(String[] args) in
   D:\Sample\IndiabixConsoleApplication\MyProgram.cs:line 20
   1. The CLR failed to handle the exception.
   2. The class MyProgram belongs to the namespace MyProgram.
   3. The function SetVal() was called from Main() in line number 20.
   4. The exception occurred in line number 26 in the function SetVal()
   5. The runtime exception occurred in the project IndiabixConsoleApplication.
    A. 1 only
    B. 1 and 2 only
    C. 3, 4 and 5 only
    D. All of the above
    E. None of the above
8. Which of the following is the Object Oriented way of handling run-time errors?
    A. OnError
    B. HERESULT
    C. Exceptions
    D. Error codes
    E. Setjump and Longjump
9. Which of the following statements is correct about the C#.NET program given below if a
   value "6" is input to it?
   using System;
   namespace IndiabixConsoleApplication
   {
     class MyProgram
      {
        static void Main(string[] args)
        {
            int index;
            int val = 44;
            int[] a = new int[5];
            try
            {
                Console.Write("Enter a number:");
                index = Convert.Tolnt32(Console.ReadLine());
                a[index] = val;
            }
            catch(FormatException e)
            {
                Console.Write("Bad Format");
            }
            catch(IndexOutOfRangeException e)
            {
                Console.Write("Index out of bounds");
            }
            Console.Write("Remaining program");
         }
      }
    }
     A. It will output: Index out of bounds Remaining program
     B. It will output: Bad Format Remaining program
     C. It will output: Bad Format
     D. It will output: Remaining program
     E. It will output: Index out of bounds
10.  Which of the following statements are correct about the exception reported below?
     Unhandled Exception: System.lndexOutOfRangeException:
     Index was outside the bounds of the array.
     at IndiabixConsoleApplication.Program.Main(String[] args) in
     D:\ConsoleApplication\Program.cs:line 14
 
     1. The program did not handle an exception called IndexOutOfRangeException.
     2. The program execution continued after the exception occurred.

3.            The exception occurred in line number 14.

4.            In line number 14, the program attempted to access an array element which was beyond the bounds of the array.

5.            The CLR could not handle the exception.

                A.            1 only

B.            1, 2 and 3 only

C.            2 and 5 only

D.            1, 3 and 4 only

E.            None of the above



________________________________________

11.          Which of the following statements are correct about exception handling in C#.NET?

1.            try blocks cannot be nested.

2.            In one function, there can be only one try block.

3.            An exception must be caught in the same function in which it is thrown.

4.            All values set up in the exception object are available in the catch block.

5.            While throwing a user-defined exception multiple values can be set in the exception, object.

                A.            1 only

B.            1 and 2 only

C.            3 only

D.            4 and 5 only

E.            All of the above



________________________________________

12.          Exceptions can be thrown even from a constructor, whereas error codes cannot be returned from a constructor.

                A.            True       B.            False



________________________________________

13.          Which of the following statements is correct about the C#.NET program given below if a value "6" is input to it?

using System;

namespace IndiabixConsoleApplication

{

    class MyProgram

    {

        static void Main (string[] args)

        {

            int index;

            int val = 66;

            int[] a = new int[5];

            try

            {

                Consote.Write("Enter a number: ");

                index = Convert.ToInt32(Console.ReadLine());

                a[index] = val;

            }

            catch(Exception e)

            {

                Console.Write("Exception occurred ");

            }

            Console.Write("Remaining program ");

        }

    }

}

                A.            It will output: Exception occurred

B.            It will output: Remaining program

C.            It will output: Exception occurred Remaining program

D.            It will output: Remaining program Exception occurred

E.            The value 66 will get assigned to a[6].



________________________________________

14.          Which of the following statements is correct about the C#.NET program given below if a value "ABCD" is input to it?

using System;

namespace IndiabixConsoleApplication

{

    class MyProgram

    {

        static void Main(string[] args)

        {

            int index;

            int val = 55;

            int[] a = new int[5];

            try

            {

                Console.Write("Enter a number: ");

                index = Convert.ToInt32(Console.ReadLine());

                a[index] = val;

            }

            catch(FormatException e)

            {

                Console.Write("Bad Format ");

            }

            catch(IndexOutOfRangeException e)

            {

                Console.Write("Index out of bounds ");

            }

            Console.Write("Remaining program ");

        }

    }

}

                A.            It will output: Bad Format

B.            It will output: Remaining program

C.            It will output: Index out of bounds

D.            It will output: Bad Format Remaining program

E.            It will output: Index out of bounds Remaining program



________________________________________

15.          All code inside finally block is guaranteed to execute irrespective of whether an exception occurs in the protected block or not.

                A.            True       B.            False

________________________________________

16.          Which of the following is NOT an Exception?

                A.            StackOverflow

B.            Division By Zero

C.            Insufficient Memory

D.            Incorrect Arithmetic Expression

E.            Arithmetic overflow or underflow



________________________________________

17.          Which of the following statements is correct about the C#.NET program given below if a value "ABCD" is input to it?

using System;

namespace IndiabixConsoleApplication

{

    class MyProgram

    {

        static void Main(string[] args)

        {

            int index;

            int vat = 88;

            int[] a = new int(5];

            try

            {

                Console.Write("Enter a number: ");

                index = Convert.Toint32(Console.ReadLine());

                a[index] = val;

            }

            catch(Exception e)

            {

                Console.Write("Exception occurred");

            }

            Console.Write("Remaining program");

        }

    }

}

                A.            It will output: Exception occurred

B.            It will output: Remaining program

C.            It will output: Remaining program Exception occurred

D.            It will output: Exception occurred Remaining program

E.            The value 88 will get assigned to a[0].



________________________________________

18.          It is compulsory for all classes whose objects can be thrown with throw statement to be derived from System.Exception class.

                A.            True       B.            False







Interview Questions:

1.  Can multiple catch blocks be executed for a single try statement?

      No.  Only one catch block get executed. Once the proper catch block processed, control is transferred to the finally block (if there are any).

2.  If I return out of a try/catch in C#, does the code in the finally-clause run?

      - Yes. The Code in the finally always runs. If you return out of the try block, or even if you do a go out of the try, the finally block always runs.

3.   Will finally block get executed if the exception had not occurred?

       Yes, always.





C# Basic Test:

1.      What is the correct syntax for the multi-line comment entries in c#?

a) // //      b)/* */      c)/@ @/     d) /  /

2.      The public keyword can be ignored for the main function in c#?

a) True        b) False

3.      The WriteLine method is a part of the ______ class.

a) System   b)System.Output   c) Console    d)Console.System

4.      C# is a _______ language.

a) Purely Procedure – Oriented

b) Partially Procedure – Oriented

c) Procedure - Oriented and Partial – oriented

d) Purely Object – Oriented

5.      Manual memory management needs to be done in c#.

a) True      b) False

6.      Access modifiers for the c# cannot be the following.

a) Protected

b) Private

c) Public

d) Public Protected

7.      In c#, an underscore is allowed as an initial character of a variable.

a)  True      b) False

8.      In c#, string is a reference type.

a) True       b) False

9.      What statement is used to completely abort the execution of a loop?

a) continueb) break

c) exit        d) goto

10.  Console.ReadLine() returns the input as a _______

a) String

b) Stream of Characters

c) Integer

d) Character

11.  In c# datatypes are divided into two fundamental categories

a) Pointers and Values

b) Value types and reference types

12.  _______ in simple terms is nothing but conversion of value type into a reference type.

a) Casting

b) Boxing

c) Unboxing

d) Overriding

13.  ______is all about converting a reference type in to value type.

a) Overloading

b) Unboxing

c) Casting

d) Boxing

14.  Unboxing requires an ________ cast.

a) explicit

b) implicit

c) implicit or explicit

d) none

15.  Basic input and output operations are performed in c# using the methods of the ______ class in the ________ namespace.

a) InputOutput, Class

b) InputOutput, System

c) Console, System

d) System, Console

16.  C# provides an unified type system, which means that all data types are derived from  ______ class.

a) System  b) Object

c) Variable d) Class

Ans: 1.b  2.a  3.c  4.d  5.b  6.d  7.a  8.a  9.b  10.a  11.b  12.b  13.b  14.a  15.c  16.b

Test #1

1. Which of the following statements are TRUE about the .NET CLR?



1. It provides a language-neutral development & execution environment.

2. It ensures that an application would not be able to access memory that it is not authorized to access.

3. It provides services to run "managed" applications.

4. The resources are garbage collected.

5. It provides services to run "unmanaged" applications.





A) Only 1 and 2     B) Only 1,2 and 4

C) 1,2,3,4               C) Only 4 and 5

2. In data reader, what can be used before read method?

(A) Getvalue               (B) Getstring

 (C) GetNumber           (D) None

3. What is ENUM?       

(A)       It is used to initialize variables

(B)       It is used to define constants

(C)       It is used to define variables

(D)       None   

4. A variable which is declared inside a method is called a________variable         

(A)       Local                (B)       Private

(C)       Static               (D)       Serial

5. Can an Interface be instantiated directly?             

(A)       Yes(B)       No

6. Which of the following are valid .NET CLR JIT performance counters?

1. Total memory used for JIT compilation

2. Average memory used for JIT compilation

3. Number of methods that failed to compile with the standard JIT

4. Percentage of processor time spent performing JIT compilation

5. Percentage of memory currently dedicated for JIT compilation    

(A)       1, 2(B)       1, 5

(C)       3, 4(D)       4, 5

7. Automatic paging is possible in     

(A)       datareader(B)       dataset

(C)       datatable   (D)       all

8. What does Dispose method do with connection object?  

(A)       Close the connection

(B)       Temporary dispose the connection

(C)       Deletes it from the memory

(D)       All of the above

9. Feature of a local variable              

(A)       It must be declared within a method

(B)       It represents a class object

(C)       It can be used anywhere in the program

(D)       It must accept a class

10. What is the Difference between Convert.ToInt32 and Int.Parse?            

(A)       Both are Same

(B)       Convert.ToInt32 Can't Handle Null Values ,it will throws rgumentNullException error.

(C)       Int.Parse Can't Handle Null values , It will throws ArgumentNullException Error.

(D)       Both can Handle Null Values

11. Which of the following statements is correct about Managed Code?     

(A)       Managed code is the code that runs on top of Windows.

(B)       Managed code is the code that is written to target the services of the CLR.

(C)       Managed code is the code where resources are Garbage Collected.

(D)       Managed code is the code that is compiled by the JIT compilers.

12. What object can help you maintain data across users?

              (A)     Session object             (B)       Server Object

              (C)     Response Object         (D)      Application Object

13.   Is it possible to change the value of a variable while debugging a C# application?

(A)       Yes(B)       No       

14.  Two methods with the same name but with different parameters.

              (A)     Overloading                (B)       Multiplexing

              (C)     Duplexing  (D)       Loading          

15.  C# doesnot support:

              (A)     abstraction(B)       polymorphism

             (C)      multiple inheritance    (D)       inheritance

16. Which of the following utilities can be used to compile managed assemblies into processor-specific native code?

               (A)    gacutil             (B)       ngen

                (C)   sn  (D)       ildasm             

17. Which property will you use to process different server paths in a page?          

                 (A)  Request           (B)       Response

                 (C)  Server              (D)       Application

18. Is it possible to change the value of a variable while debugging a C# application?

                 (A)  Yes(B)       No

19. Is there any errors in this -> EmployeeMgmt constructor: Public int EmployeeMgmt { emp_id = 100; }

                 (A)  Return type     (B)       Formal parameters

                 (C)  No errors         (D)       Name

20. Which is true about Interface and abstract methods?

(A) We can write only one abstract method inside interface.

(B) No method is abstract inside interface

(C) All the methods inside Interface in an abstract method.

(D) None of the above




ANSWERS:
1).C   2).D   3).B    4).A   5).B   6).C   7).C   8).C   9).A   10).C   11).B   12).D   13).A   14).A   15).C   16).B   17).C   18).A  19).A   20).C     
 
 
 

3 comments: