MCQs on Basic Syntax and Data Types | C#

Explore 30 MCQs on C# Basic Syntax and Data Types, covering syntax, data types (value and reference), type conversions, casting, and more. Strengthen your foundation in C# programming with these questions.


Chapter 2: Basic Syntax and Data Types

C# Syntax Basics

  1. What symbol is used to terminate a statement in C#?
    • a) .
    • b) ;
    • c) :
    • d) ,
  2. Which of the following is a valid expression in C#?
    • a) int x = 10
    • b) 10 + 20
    • c) if (x > 10)
    • d) All of the above
  3. What is the purpose of the using directive in C#?
    • a) To import namespaces
    • b) To define a method
    • c) To declare variables
    • d) To define classes
  4. How do you declare a variable in C#?
    • a) var x = 10;
    • b) x = 10;
    • c) int x = 10
    • d) declare x as int
  5. Which of the following are valid comments in C#?
    • a) // This is a comment
    • b) /* This is a comment */
    • c) <!-- This is a comment -->
    • d) Both a and b
  6. What is the purpose of comments in C#?
    • a) To write descriptions and explanations in the code
    • b) To improve the program’s execution speed
    • c) To execute a block of code
    • d) To declare variables
  7. Which comment style is used for a single line comment in C#?
    • a) /* comment */
    • b) // comment
    • c) <!-- comment -->
    • d) # comment
  8. How do you define a multi-line comment in C#?
    • a) // comment
    • b) # comment
    • c) /* comment */
    • d) <!-- comment -->
  9. Which of the following is the correct syntax for a conditional statement in C#?
    • a) if (x > 10) { }
    • b) if x > 10 { }
    • c) if { x > 10 }
    • d) if (x > 10) : { }
  10. What does the Console.WriteLine() method do in C#?
  • a) Prints a message to the console
  • b) Reads input from the user
  • c) Compiles the program
  • d) Declares variables

Data Types in C#

  1. Which of the following is a value type in C#?
  • a) string
  • b) int
  • c) object
  • d) array
  1. What is the default value of an int type in C#?
  • a) 0
  • b) null
  • c) false
  • d) "" (empty string)
  1. Which of the following is the correct way to declare a bool variable in C#?
  • a) bool isTrue = true;
  • b) boolean isTrue = true;
  • c) bool isTrue = "true";
  • d) boolean isTrue = "true";
  1. What type is used to represent a floating-point number in C#?
  • a) int
  • b) double
  • c) char
  • d) bool
  1. Which C# type is used to store a single character?
  • a) string
  • b) char
  • c) int
  • d) float
  1. Which data type is used for storing large decimal values with precision in C#?
  • a) float
  • b) double
  • c) decimal
  • d) int
  1. What type does the string keyword represent in C#?
  • a) Reference type
  • b) Value type
  • c) Object type
  • d) Enum type
  1. What does the char data type represent in C#?
  • a) A single character from the ASCII table
  • b) A string of characters
  • c) A boolean value
  • d) A 16-bit integer value
  1. How is an array declared in C#?
  • a) int[] arr = new int[5];
  • b) int arr[5];
  • c) array<int> arr = new array<int>(5);
  • d) arr = new int[5];
  1. Which of the following is a reference type in C#?
  • a) int
  • b) bool
  • c) string
  • d) double

Type Conversion and Casting

  1. What is the difference between implicit and explicit casting in C#?
  • a) Implicit casting happens automatically, explicit casting requires a cast operator
  • b) Implicit casting requires a cast operator, explicit casting happens automatically
  • c) Both are the same
  • d) Both require a special function
  1. Which of the following is an example of implicit casting in C#?
  • a) int x = 10; double y = x;
  • b) double x = 10.5; int y = (int)x;
  • c) float x = 10.5; string y = (string)x;
  • d) char x = 'a'; int y = (int)x;
  1. What is the result of trying to assign a double value to an int without casting?
  • a) It will result in a compile-time error
  • b) The value will be automatically rounded down
  • c) It will throw a runtime exception
  • d) The double will be converted to a string
  1. How do you perform explicit casting in C#?
  • a) By using the cast operator (type)
  • b) By using the Convert.ToType() method
  • c) By using the int() method
  • d) By using parse() function
  1. What is the purpose of the Convert.ToInt32() method in C#?
  • a) It converts a value to an integer type
  • b) It performs implicit casting from double to int
  • c) It checks if a string is a valid integer
  • d) It adds 32 to the integer value
  1. What happens during boxing in C#?
  • a) A value type is converted to a reference type
  • b) A reference type is converted to a value type
  • c) A string is converted to a boolean
  • d) A reference type is copied into an array
  1. What is unboxing in C#?
  • a) Converting a reference type back into a value type
  • b) Converting a value type back into a reference type
  • c) Converting an array into an object
  • d) Converting a reference type to a string
  1. Which type of casting is performed when converting a double to an int with truncation in C#?
  • a) Implicit casting
  • b) Explicit casting
  • c) Auto-casting
  • d) Unboxing
  1. What is the result of casting a null reference type to any value type in C#?
  • a) It will result in a runtime exception
  • b) The null will be converted to 0
  • c) The null will be automatically boxed
  • d) It will automatically cast to string
  1. How do you safely convert a string to an integer in C# without throwing an exception?
  • a) Using Convert.ToInt32()
  • b) Using int.Parse()
  • c) Using int.TryParse()
  • d) Using int.Convert()

Answer Key

QnoAnswer
1b) ;
2d) All of the above
3a) To import namespaces
4c) int x = 10
5d) Both a and b
6a) To write descriptions and explanations in the code
7b) // comment
8c) /* comment */
9a) if (x > 10) { }
10a) Prints a message to the console
11b) int
12a) 0
13a) bool isTrue = true;
14b) double
15b) char
16c) decimal
17a) Reference type
18a) A single character from the ASCII table
19a) int[] arr = new int[5];
20c) string
21a) Implicit casting happens automatically, explicit casting requires a cast operator
22a) int x = 10; double y = x;
23a) It will result in a compile-time error
24a) By using the cast operator (type)
25a) It converts a value to an integer type
26a) A value type is converted to a reference type
27a) Converting a reference type back into a value type
28b) Explicit casting
29a) It will result in a runtime exception
30c) Using int.TryParse()

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top