All topics
C# Basics
Program structure, console output, variables, built-in types, operators, type conversions and strings.
Sort:
11 questions
JuniorTheoryCommonWhat does the program print (lines separated by spaces)?
JuniorTheoryCommon
What does the program print (lines separated by spaces)?
2 3
Open full question →Common mistakes
- ✗Expecting
3 3:a = bcopies the number2intoaand does not tieatob, so changingbafterwards leavesaalone. - ✗Expecting
1 3:=copies the value on its right into the variable on its left, soa = bchangesa, notb.
Follow-up questions
- →What would the program print if the line
b = 3;came beforea = b;? - →How would you swap the values of
aandbwith the help of a third variable?
JuniorTheoryCommonWhat happens when this program is compiled and run?
JuniorTheoryCommon
What happens when this program is compiled and run?
It does not compile
Open full question →Common mistakes
- ✗Expecting
Startto print before the error: an unknown name is a compile error, so the program never runs and prints no line at all. - ✗Expecting C# to ignore letter case:
consoleandConsoleare different names, and the compiler knows onlyConsole.
Follow-up questions
- →What error code does the compiler report for
console, and what does its message say? - →What does the compiler report if the second line calls
Console.Writelineinstead?
JuniorTheoryCommonWhat does the program print (lines separated by spaces)?
JuniorTheoryCommon
What does the program print (lines separated by spaces)?
3 -3
Open full question →Common mistakes
- ✗Expecting
4 -4: a cast tointdoes not round to the nearest whole number; it drops the fraction, so3.99becomes3. - ✗Expecting
-4for the negative number: dropping the fraction moves a number toward zero, so-3.99becomes-3, not-4.
Follow-up questions
- →What does
Convert.ToInt32(3.99)return, and why does it differ from the cast? - →Why does
int x = 3.99;not compile whileint x = (int)3.99;does?
JuniorTheoryCommonWhat does the program print (lines separated by spaces)?
JuniorTheoryCommon
What does the program print (lines separated by spaces)?
False True
Open full question →Common mistakes
- ✗Expecting
0.1 + 0.2 == 0.3to betrue:doublestores binary fractions, so the sum is0.30000000000000004. - ✗Expecting
decimalto miss as well: it stores decimal digits, so0.1m + 0.2mis exactly0.3m.
Follow-up questions
- →How would you check that two
doublevalues are equal within a tolerance? - →Why is
decimalpreferred for money even though it is slower thandouble?
JuniorTheoryCommonWhat happens when this program is compiled and run?
JuniorTheoryCommon
What happens when this program is compiled and run?
It does not compile
Open full question →Common mistakes
- ✗Expecting
2: C# never drops a fraction silently, so putting2.5into anintstops the build with errorCS0266. - ✗Expecting an error only at run time: the compiler already sees a
doublegoing into anintvariable, so no program is built and nothing runs.
Follow-up questions
- →How would you store
2.5in aninton purpose, dropping the fraction? - →Why does
double y = 2;compile whenint x = 2.5;does not?
JuniorTheoryCommonWhat does the program print (lines separated by spaces)?
JuniorTheoryCommon
What does the program print (lines separated by spaces)?
3 -1
Open full question →Common mistakes
- ✗Expecting
3.5: dividing oneintby another drops the fraction, so7 / 2is3. - ✗Expecting a non-negative remainder, as in math: in C# the remainder takes the sign of the dividend, so
-7 % 3is-1.
Follow-up questions
- →How would you change the first line so that it prints
3.5? - →What does
7 % -3evaluate to, and why?
JuniorTheoryCommonWhat does the program print?
JuniorTheoryCommon
What does the program print?
-2147483648
Open full question →Common mistakes
- ✗Expecting
2147483648:max + 1is aninttoo, and anintcannot hold that number, so the value wraps around toint.MinValue. - ✗Expecting an
OverflowException: arithmetic on variables is unchecked by default, and onlycheckedmakes an overflow throw.
Follow-up questions
- →What happens if you write
checked(max + 1)instead? - →Why does
Console.WriteLine(int.MaxValue + 1);fail to compile?
JuniorTheoryCommonWhat does the program print (lines separated by spaces)?
JuniorTheoryCommon
What does the program print (lines separated by spaces)?
False 0
Open full question →Common mistakes
- ✗Expecting
False 1:&&stops as soon as its left side isfalse, so++nnever runs andnstays0. - ✗Expecting
True 1, as if++nran first: C# always evaluates the left side of&&first, andn > 0isfalse.
Follow-up questions
- →What does the program print if
&&is replaced with&? - →What does it print when the left side is
n >= 0instead?
JuniorTheoryCommonWhat does the program print (lines separated by spaces)?
JuniorTheoryCommon
What does the program print (lines separated by spaces)?
abc ABC
Open full question →Common mistakes
- ✗Expecting
ABCon the first line:ToUpperdoes not changes; it returns a new string, and a call without an assignment loses it. - ✗Expecting
s = s.ToUpper()to fail or do nothing: the string is immutable, but the variable can take a new string at any time.
Follow-up questions
- →Which other string methods behave like
ToUpperhere? - →What does the compiler say about
s[0] = 'A';?
JuniorTheoryCommonWhat does the program print?
JuniorTheoryCommon
What does the program print?
board
Open full question →Common mistakes
- ✗Expecting
bo: the second argument ofSubstringis the length of the piece, not the index of its end, soSubstring(3, 5)takes five characters. - ✗Expecting an exception: the piece may end at the last character, so a start of 3 and a length of 5 fit an 8-letter string exactly.
Follow-up questions
- →What does
s.Substring(3)print for the same string? - →What happens if the call is changed to
s.Substring(3, 6)?
JuniorTheoryCommonWhat does the program print (lines separated by spaces)?
JuniorTheoryCommon
What does the program print (lines separated by spaces)?
AB CD
Open full question →Common mistakes
- ✗Expecting
A B C D:Writeprints its text and leaves the cursor on the same line, soAandBend up together on the first line. - ✗Expecting the line break before the text:
WriteLineprints its text first and only then ends the line, soCstarts the second line.
Follow-up questions
- →What does
Console.WriteLine()with empty parentheses print? - →Why do
WriteandWriteLineprint two words without a space between them?