Basics Questions with Answers

12 practice questions on Basics (C#) with answers and step-by-step solutions. Try 10 samples below, then practise all 12 free in the AptiNest app.

Sample questions

Question 1Easy

In C#, how is the keyword string related to System.String?

  1. string is a value type; String is a class
  2. string is an alias for System.String
  3. string is a primitive with no methods
  4. string is a separate type from System.String

Answer: B. string is an alias for System.String

Explanation

string is the C# keyword that maps to System.String, which is a reference type.

Step-by-step solution

  1. The compiler maps keyword string.
  2. It resolves to System.String.
  3. Both mean the same type.

Why the other options are wrong

  • A. Both are the same reference type.
  • C. string has all the String methods, such as Length and Substring.
  • D. They are literally the same type; the keyword is shorthand.

Question 2Easy

What is printed by this code?

int x = 7 / 2;
Console.WriteLine(x);
  1. 3
  2. 3.5
  3. 4
  4. It does not compile

Answer: A. 3

Explanation

Dividing two ints gives an int, so 7 / 2 is 3 with the fraction dropped.

Step-by-step solution

  1. Both operands are int.
  2. Integer division truncates toward zero.
  3. x is 3.

Why the other options are wrong

  • B. 3.5 needs a double, but both operands are int.
  • C. Integer division truncates and does not round up.
  • D. Assigning an int division to an int is legal.

Tip: Use 7 / 2.0 for 3.5.

Question 3Medium

Which of the following is the correct default value of a Boolean type?

  1. 0
  2. 1
  3. True
  4. False

Answer: D. False

Explanation

Boolean types return only true and false but default value of boolean type will be false.

Step-by-step solution

  1. The default value of a bool variable or field is false.
  2. C# gives every bool the default value False, not 0 or True.
  3. So the correct answer, False, is correct.

Why the other options are wrong

  • A. 0 is a number, not a bool value.
  • B. 1 is not a boolean value in C#.
  • C. True is not the default; the default is False.

Tip: Default values: bool is false, int is 0, reference types are null.

Question 4Medium

Arrange following datatype in order of increasing magnitude sbyte, short, long, int.

  1. long< short <int<sbyte
  2. sbyte< short <int< long
  3. short<sbyte<int< long
  4. short<int<sbyte< long

Answer: B. sbyte< short <int< long

Explanation

The sizes are sbyte 1 byte, short 2 bytes, int 4 bytes and long 8 bytes, so the order of increasing size is sbyte, short, int, long.

Step-by-step solution

  1. Sbyte range = -128 to 127.
  2. Short range = -32768 to 32767.
  3. Int range = -2147483648 to 2147483647.
  4. Long range = -9223372036854775808 to 9223372036854775807.
  5. Sbyte < short < int < long.

Why the other options are wrong

  • A. Long is largest, not smallest.
  • C. Short is smaller than sbyte.
  • D. Sbyte is smaller than int.

Tip: Remember the order: sbyte < short < int < long based on size.

Question 5Medium

What is the difference between const and readonly fields?

  1. const is fixed at compile time; readonly can be set in a constructor
  2. const can be assigned in the constructor; readonly can never be set after declaration
  3. Both are evaluated at run time only and behave in the same way
  4. readonly applies only to local variables

Answer: A. const is fixed at compile time; readonly can be set in a constructor

Explanation

A const must have a compile-time value. A readonly field can get a value at declaration or in a constructor, so different instances may differ.

Step-by-step solution

  1. const: compile-time constant.
  2. readonly: set once during construction.

Why the other options are wrong

  • B. It is reversed: const cannot be set in a constructor.
  • C. const values are baked in at compile time.
  • D. readonly applies to fields, not locals.

Question 6Medium

What would be the output of the following program?

using System;
namespace comp
{
 class DataTypes
 {
 static void Main(string[] args)
 {
  int i;
   Console.WriteLine("i is not used   in this program!");
    }
 }
}
  1. i is not used in this program!
  2. Compilation Error
  3. No output
  4. None of the Above

Answer: A. i is not used in this program!

Explanation

If a variable is not initialized but is not used anywhere, there is no issue and the program executes perfectly fine.
The error comes only when we try to access the value of an uninitialized variable.

Step-by-step solution

  1. The program declares int i but never reads it.
  2. C# reports an error only when an uninitialized variable is read, not when it is merely declared.
  3. The WriteLine prints the text, so the marked output, 'i is not used in this program!', is correct.

Why the other options are wrong

  • B. Compilation succeeds because i is never read.
  • C. The WriteLine call prints the text, so there is output.
  • D. Option A is the correct output.

Tip: Declaring a variable without reading it is fine; the compiler only objects when an unassigned variable is read.

Question 7Medium

What is wrong with the following code?

static void Main(string[] args)
{
    const int m = 100;
    int n = 10;
    const int k = n / 5 * 100 * n;
    Console.WriteLine(m * k);
    Console.ReadLine();
}
  1. k should not be declared constant
  2. Expression assigned to "k" should be constant in nature
  3. Expression (m * k) is invalid
  4. "m" is declared in invalid format

Answer: B. Expression assigned to "k" should be constant in nature

Explanation

"k" should be declared as const int k = 10/5 * 100*10 i.e only constant values should be assigned to a constant.

Step-by-step solution

  1. A const declaration needs a value that is known at compile time.
  2. N is a variable set at run time, so n / 5 * 100 * n is not a constant expression.
  3. So the correct answer, the expression assigned to k must be constant, is correct.

Why the other options are wrong

  • A. K can be const; the problem is the value assigned to it.
  • C. M * k is a valid expression; the error is in how k is declared.
  • D. M is declared correctly as a const int.

Tip: Values given to const must be literals or other constants.

Question 8Medium

What does the using statement guarantee for an IDisposable object?

  1. Dispose is called at block end, even after an exception
  2. The object is never garbage collected, even after Dispose runs
  3. The object is created lazily on its first member access
  4. It is copied to the stack

Answer: A. Dispose is called at block end, even after an exception

Explanation

using expands to a try/finally that calls Dispose(), so files, connections and similar resources are released reliably.

Step-by-step solution

  1. Resource is created.
  2. Block runs.
  3. Dispose runs in finally.

Why the other options are wrong

  • B. Garbage collection is unaffected.
  • C. Creation is not lazy.
  • D. It stays a reference and is not copied to the stack.

Question 9Medium

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.

  1. Only 1 and 2
  2. Only 1, 2 and 4
  3. 1, 2, 3, 4
  4. Only 4 and 5

Answer: C. 1, 2, 3, 4

Explanation

CLR (Common Language Runtime) is a heart of Dot Net Framework.

It is a core runtime environment of .NET Framework for executing applications. The main function of Common Language Runtime (CLR) is to convert the Managed Code into native code and then execute the Program. It acts as a layer between Operating Systems and the applications written in. Net languages.

CLR handles the execution of code and provides useful services for the implementation of the program. In addition to executing code, CLR provides services such as memory management, thread management, security management, code verification, compilation, and other system services

Step-by-step solution

  1. Statement 1 is true: the CLR gives a language-neutral environment.
  2. Statement 2 is true: it stops code from reaching memory it is not allowed to use.
  3. Statement 3 is true: it runs managed applications, and statement 4 is true: resources are garbage collected.
  4. Statement 5 is false: the CLR runs managed code, not unmanaged code.
  5. So statements 1, 2, 3 and 4 are true, which is option C.

Why the other options are wrong

  • A. Statements 3 and 4 are also true, so 1 and 2 alone is incomplete.
  • B. Statement 3 is also true, so 1, 2 and 4 is incomplete.
  • D. Statement 4 is true and statement 5 is false, so 4 and 5 is wrong.

Tip: The CLR runs managed code and provides garbage collection, memory safety and language neutrality.

Question 10Hard

What is printed?

int i = 5;
object o = i;
i = 10;
Console.WriteLine(o);
  1. 10
  2. 15
  3. 5
  4. It does not compile

Answer: C. 5

Explanation

Boxing copies the value into a new heap object. Changing i afterwards does not touch the boxed copy, so o is still 5.

Step-by-step solution

  1. Boxing copies 5 into o.
  2. i changes to 10.
  3. o still holds 5.

Why the other options are wrong

  • A. The boxed copy does not follow later changes to i.
  • B. Nothing adds the two values.
  • D. Boxing an int into object is implicit and legal.

Practise all 12 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all C# topics · All topics