quiz Computer Science · 19 questions

C# Data Types and Arrays

help_outline 19 questions
timer ~10 min
auto_awesome AI-generated
0 / 19
Score : 0%
1

When reading user input in C#, why must the input be converted before assigning to a numeric variable?

2

What will be the value of the variable `myNum` after executing `int myNum = myArray[3];` given the array `{1,2,3,4,5,6}`?

3

Which of the following statements about casting an `int` to a `long` is true?

4

What happens when you try to cast a `long` value larger than `int.MaxValue` to an `int`?

5

Which data type uses 2 bytes per character and stores a sequence of characters?

6

If an array of type `int[]` is declared but not initialized, what is the default value of each element?

7

Which of the following statements correctly converts a `float` to an `int` without using `Parse`?

8

What is the result of executing `Console.WriteLine(5 / 2);` in C#?

9

Which statement best describes why adding a `?` after a type name (e.g., `string?`) removes a squiggly warning in Visual Studio?

10

When accessing an array element beyond its declared size, what does C# typically do?

11

Which of the following data types occupies the smallest amount of memory?

12

If you declare `float f = 3.14;` without a suffix, what compile‑time error will you receive?

13

Which method is used to convert a non‑string value to its string representation in C#?

14

What is the effect of declaring an array as `int[] numbers = new int[5];` without assigning values to its elements?

15

Which of the following best explains why `int` cannot store fractional numbers?

16

When converting a `string` containing "123" to an `int` using `int.Parse`, what exception is thrown if the string is "abc"?

17

Which statement accurately describes the memory layout of a `char` in C#?

18

If you attempt to store a `string` value into an `int` array, what compile‑time error will you encounter?

19

What is the primary reason for using iteration (e.g., loops) when working with arrays, as hinted in the material?

menu_book

C# Data Types and Arrays

Review key concepts before taking the quiz

Understanding C# Data Types and Arrays

In the world of C# programming, mastering data types and array handling is essential for writing clean, efficient, and bug‑free code. This course breaks down the most common questions that appear on quizzes, turning each one into a short lesson packed with examples, memory tricks, and practical tips. Whether you are a beginner learning how Console.ReadLine works or an experienced developer reviewing casting rules, you will find clear explanations that improve both your knowledge and your search‑engine visibility.

Why Convert Console Input Before Assigning to a Numeric Variable?

Console.ReadLine always returns a string. The .NET runtime cannot automatically place that text into an int, float, or any other numeric type because the underlying representation is different. You must explicitly convert the string to a number using methods such as int.Parse, int.TryParse, or Convert.ToInt32.

Example:

string input = Console.ReadLine(); // "42"
int number = int.Parse(input);   // now number == 42

Memory trick: Think of the string as a word and the numeric variable as a number. Just as you would translate a word into a number before doing math, you must translate the text you typed into a numeric value before the compiler will accept it.

Array Indexing Basics: Accessing Elements Correctly

C# arrays are zero‑based. This means the first element lives at index 0, the second at 1, and so on. Given an array int[] myArray = {1,2,3,4,5,6};, the expression myArray[3] retrieves the fourth element, which is 4.

Visual picture:

  • Index 0 → 1
  • Index 1 → 2
  • Index 2 → 3
  • Index 3 → 4
  • Index 4 → 5
  • Index 5 → 6

Trap warning: Forgetting the zero‑based rule often leads to off‑by‑one errors, especially when looping through an array with for (int i = 0; i < myArray.Length; i++).

Implicit Casting: Converting int to long

When you assign an int value to a long variable, C# performs an implicit widening conversion. No explicit cast is required because a long (64‑bit) can safely contain any int (32‑bit) value.

Example:

int small = 12345;
long big = small; // implicit conversion, no cast needed

Memory analogy: Pouring water from a small cup into a larger bucket – the amount of water stays the same, you just have more room.

Narrowing Casting: What Happens When a long Exceeds int.MaxValue?

Casting a long that is larger than int.MaxValue to an int causes overflow. The high‑order 32 bits are discarded, and the remaining bits are interpreted as a signed 32‑bit integer, often resulting in a negative number.

Example:

long huge = 3_000_000_000L; // larger than int.MaxValue (2,147,483,647)
int narrowed = (int)huge;   // results in -1294967296 (wrap‑around)

Visual metaphor: Imagine a clock hand that spins past 12 and lands on a lower number – the value “wraps around”.

Trap warning: The compiler allows the cast, but the runtime does not raise an exception. Use checked blocks if you need overflow detection.

String Type: Storing Sequences of Characters

In C#, the string type represents a sequence of Unicode characters. Each character occupies 2 bytes (UTF‑16 encoding) in memory. Unlike char, which stores a single character, string can hold an entire sentence, file path, or any textual data.

Example:

string greeting = "Hello, world!";
Console.WriteLine(greeting.Length); // prints 13

Because strings are immutable, any modification creates a new string object, which is an important performance consideration when working with large texts.

Default Values in Uninitialized int[] Arrays

If you declare an array of value types (e.g., int[] numbers = new int[5];) without explicitly assigning values, each element is automatically initialized to its default value. For int, the default is 0.

Example:

int[] numbers = new int[3]; // {0, 0, 0}
Console.WriteLine(numbers[1]); // prints 0

This behavior helps avoid undefined memory errors that are common in lower‑level languages.

Converting float to int Without Using Parse

The most direct way to convert a floating‑point number to an integer is by using an explicit cast. This operation truncates the fractional part, effectively rounding toward zero.

Correct statement: int result = (int)myFloat;

Example:

float myFloat = 7.9f;
int result = (int)myFloat; // result == 7

Other approaches such as Convert.ToInt32 perform rounding, while int.Parse expects a string, not a numeric type.

Integer Division: What Does 5 / 2 Produce?

When both operands of the division operator are integers, C# performs integer division. The fractional part is discarded, so 5 / 2 yields 2, not 2.5 or 3.

Example:

int result = 5 / 2; // result == 2
Console.WriteLine(result);

If you need a floating‑point result, cast one operand to double or float first: (double)5 / 2 yields 2.5.

Putting It All Together: Quick Review Checklist

  • Console input → always a string; use Parse or TryParse to convert.
  • Array indices start at 0; myArray[3] accesses the fourth element.
  • Widening cast from int to long needs no explicit cast.
  • Narrowing cast from long to int can overflow and wrap around.
  • String stores Unicode characters using 2 bytes per character.
  • Default array values for int are 0.
  • Float‑to‑int conversion uses an explicit cast, which truncates.
  • Integer division discards the remainder; use a floating‑point cast for decimal results.

By internalizing these concepts, you will reduce common bugs, write more readable code, and improve your performance in both quizzes and real‑world C# projects.

Further Learning Resources

To deepen your understanding, explore the official Microsoft documentation on C# built‑in types, read the Arrays guide, and practice casting scenarios on platforms like dotnetfiddle.net. Consistent practice will turn these rules into second nature.

Stop highlighting.
Start learning.

Join students who have already generated over 50,000 quizzes on Quizly. It's free to get started.