Unit 2: C# Programming Fundamentals - Practice Quiz
1 Which C# data type is commonly used to store a whole number?
2 Which symbol is used to assign a value to a variable in C#?
3 Which method displays text on the console and moves to the next line?
4 Which method is used to read a complete line of input from the console?
5
Which statement prints Hello, World! in a C# console program?
6 Which statement is used to execute code when a condition is true?
7
Which keyword provides an alternative block when an if condition is false?
8 Which loop is commonly used when the number of repetitions is known?
9 Which loop checks its condition before executing its body?
10 Which jump statement immediately exits a loop?
11 Which statement skips the remaining statements in the current loop iteration?
12 What is the index of the first element in a C# array?
13 Which property returns the number of elements in a C# array?
14 Which method converts all letters in a string to uppercase?
15 Which property returns the number of characters in a string?
16 Which collection stores elements as key-value pairs?
17 What is a method in C#?
18 What is an object in C#?
19 When is a constructor usually called?
20 Which access modifier allows a member to be accessed from any code that can see the class?
21
What is the value of result after this code executes?
int count = 5;
double result = count / 2;
2.0
3.0
2.5
22
Which statement correctly reads an integer entered by the user and stores it in age?
int age = Console.Read();
int age = Console.ReadLine();
int age = Convert.ToInt32(Console.ReadLine());
int age = Parse(Console.ReadLine());
23
Which statement is required to display Hello, World! in a standard C# console program?
System.Print("Hello, World!");
Print.Console("Hello, World!");
Console.Display("Hello, World!");
Console.WriteLine("Hello, World!");
24
What is printed by the following code?
int marks = 72;
if (marks >= 80)
Console.Write("A");
else if (marks >= 60)
Console.Write("B");
else
Console.Write("C");
C
A
B
25
How many times is Console.Write(i); executed?
for (int i = 1; i <= 10; i += 2)
{
Console.Write(i);
}
26
What is printed by this code?
for (int i = 1; i <= 5; i++)
{
if (i == 3)
continue;
Console.Write(i);
}
345
12
12345
1245
27
What is the value of numbers[2] after this code executes?
int[] numbers = { 4, 8, 12, 16 };
numbers[2] = numbers[0] + numbers[1];
20
24
12
16
28
What is the value of text after this code executes?
string text = " CSharp ";
text = text.Trim().ToUpper();
" CSHARP "
"CSHARP"
"csharp"
" CSharp "
29 Which collection is most appropriate when items must be accessed by a unique string key?
Dictionary<string, int>
List<int>
Stack<double>
Queue<string>
30
What is printed by the following code?
static void Update(int x)
{
x = 20;
}
int value = 10;
Update(value);
Console.Write(value);
10
20
0
31
Which code correctly creates an object from the following class?
class Student
{
public string Name;
}
object s = create Student();
Student s = new Student();
Student s = Student();
Student s = new Student;
32
What is the purpose of the constructor in this class?
class Product
{
public string Code;
public Product(string code)
{
Code = code;
}
}
33 A field should be accessible only inside its own class. Which access modifier provides this restriction?
public
protected
internal
private
34 Which design best demonstrates encapsulation for a bank account balance?
balance and calculate it when needed
balance private and update it through methods
balance in a global variable
balance public and change it anywhere
35 Which situation best illustrates abstraction?
36
Given class Dog : Animal, which statement is correct?
Animal inherits from Dog
Dog can inherit accessible members from Animal
Dog cannot define its own methods
Animal must be declared as a structure
37
What concept is demonstrated when a derived class provides its own implementation of a base class method declared with virtual?
38 Which block executes whether or not an exception occurs?
finally
throw
try
catch
39
Which statement correctly reads all text from a file named data.txt?
File.OpenTextAll("data.txt")
File.ReadAllText("data.txt")
File.GetAllText("data.txt")
File.ReadText("data.txt")
40 A program produces the wrong result but runs without crashing. Which debugging action is most useful for locating the cause?
41
What does the following C# code print?
byte b = 250;
try
{
checked
{
b += 10;
}
Console.Write($"V{b}");
}
catch (OverflowException)
{
Console.Write($"E{b}");
}
E4
V4
E250
V260
42
Standard input contains exactly one empty line followed by end-of-file. What does this code print?
string? a = Console.ReadLine();
string? b = Console.ReadLine();
Console.Write($"{a?.Length ?? -1},{b?.Length ?? -1}");
1,0
-1,-1
0,0
0,-1
43
In a C# 12 console project, what is written to standard output by this program?
Console.Write("T");
class Program
{
public static void Main() => Console.Write("M");
}
TM
T
M
44
What value is printed?
int x = 0;
bool A() { x += 1; return false; }
bool B() { x *= 10; return true; }
if (A() && B() || B())
x += 3;
Console.Write(x);
103
3
13
10
45
What value does the following loop print?
int sum = 0;
for (int i = 0; i < 4; i++)
{
switch (i)
{
case 1: continue;
case 2: break;
}
sum += i;
}
Console.Write(sum);
5
6
2
3
46
What does this program print?
static int F()
{
int x = 1;
try
{
return x;
}
finally
{
x = 2;
Console.Write(x);
}
}
Console.Write(F());
22
11
21
12
47
What does the following code print?
object[] values = new string[2];
values[0] = "ok";
try
{
values[1] = new object();
Console.Write("A");
}
catch (ArrayTypeMismatchException)
{
Console.Write("B");
}
Console.Write(values[0]);
Bok
Aok
BObject
AObject
48
What does this code print?
string s = "A\U0001F600B";
string t = s.Substring(1, 2);
Console.Write($"{s.Length}:{t.Length}:{char.IsSurrogatePair(t, 0)}");
3:1:True
3:2:False
4:2:True
4:1:False
49
What does the following code print?
var values = new List<int> { 1, 2, 3 };
try
{
foreach (int value in values)
{
if (value == 2)
values.Remove(value);
Console.Write(value);
}
}
catch (InvalidOperationException)
{
Console.Write("E");
}
123E
1E
123
12E
50
What values are printed?
static void Update(int a, ref int b, out int c)
{
a++;
b += a;
c = a + b;
}
int x = 2;
Update(x, ref x, out int y);
Console.Write($"{x},{y}");
2,7
5,8
5,7
3,6
51
What does this object-initializer code print?
class Counter
{
private int x;
public int X
{
get => x;
set { x = value; Y = x + 1; }
}
public int Y { get; set; }
}
var c = new Counter { Y = 10, X = 3 };
Console.Write($"{c.X},{c.Y}");
4,10
3,4
3,10
4,11
52
What is the output when new C() executes?
class C
{
public C() : this(1) { Console.Write("A"); }
private C(int n) : this("x") { Console.Write("B"); }
private C(string s) { Console.Write("C"); }
}
CAB
BCA
ABC
CBA
53
Assembly A defines the following class:
public class Base
{
private protected int P;
protected internal int Q;
}
Assembly B, which has no friend-assembly access to A, defines class Derived : Base. Inside an instance method of Derived, variables Base b and Derived d are available. Which statement compiles?
this.P = 1;
b.Q = 1;
b.P = 1;
d.Q = 1;
54
A class stores data in private readonly List<int> _items. A view obtained by a caller must reflect later internal additions, but callers must not be able to mutate the list through that view. Which implementation best satisfies both requirements?
_items.AsReadOnly() wrapper.
_items as an IReadOnlyList<int>.
_items.ToArray() snapshot.
_items.ToList() copy.
55
Consider this C# 12 code:
interface I
{
void M() => Console.Write("I");
}
abstract class A : I { }
class B : A { }
Which statement correctly describes the two expressions ((I)new B()).M() and new B().M()?
I.
B does not implement M.
I; the second does not compile.
I.
56
What does this program print?
class Base
{
public string Name() => "B";
}
class Derived : Base
{
public new string Name() => "D";
}
Derived d = new Derived();
Base b = d;
Console.Write(d.Name() + b.Name());
DD
DB
BB
BD
57
What does the following code print?
class Base
{
public virtual string F(object value) => "BO";
}
class Derived : Base
{
public override string F(object value) => "DO";
public string F(string value) => "DS";
}
Base x = new Derived();
Console.Write(x.F("text"));
DS
DO
BO
58
What value is printed?
int x = 0;
try
{
throw new InvalidOperationException();
}
catch (Exception) when (++x == 0)
{
x += 100;
}
catch (InvalidOperationException)
{
x += 10;
}
finally
{
x *= 2;
}
Console.Write(x);
20
202
22
2
59
Assume path is writable and initially unused. What content is printed?
File.WriteAllText(path, "abcdef");
using (var stream = new FileStream(path, FileMode.Open, FileAccess.Write))
{
stream.WriteByte((byte)'X');
}
Console.Write(File.ReadAllText(path));
abcdefX
Xbcdef
IOException is thrown
X
60
A debugger must retain the original exception throw site after the exception is logged in a catch block. Which replacement for RETHROW satisfies that requirement?
catch (Exception ex)
{
Log(ex);
RETHROW
}
throw new Exception(ex.Message);
throw new Exception("failed", ex);
throw;
throw ex;
Did this save you a night before the exam?
LPU Notes is free, and it stays free. Ads cover part of the server bill. The rest comes out of a student's own pocket: the domain, the storage, and keeping the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason. to try again.
Nothing here is ever locked, and nothing unlocks. Chip in only if it was worth it. What it pays for →