Syntax, Output, Comments, Variables, Data Types, Type Casting, User Input, Operators Flashcards

1
Q

Write a “Hello World!” Program that outputs “Hello World!”.

A

using System;
namespace HelloWorld {
class Program {
static void main(string[] args) {
Console.WriteLine(“Hello World!”);
}
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

You have the following format of C# code:
using System;
namespace HelloWorld {
class Program {
static void main(string[] args) {
Console.WriteLine(“Hello World!”);
}
}
}
1. Explain line 1
2. Explain line 2
3. Explain line 3
4. Explain line 4
5. Explain line 5

A
  1. using System means that we can use classes from the System namespace. Tells the compiler that the program is using the System namespace, which includes fundamental classes necessary for basic operations, like input/output.
  2. namespace is used to organize the code, and it is a container for classes and other namespaces.
  3. class is a container for data and methods, which brings functionality to our programs.
  4. This is the main entry point of any C# console application. The Main method must be static, and it typicaly returns void (though it can also return int (return 0;)). string[] args represents an array of command line arguments that can be passed to the program when it is executed.
  5. Console is a class of the System namespace, which has a WirteLine() ethod that is used to output/print text.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Name 2 ways to output text in C#.

A

WriteLine() and Write() methods.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Describe the WriteLine() Method and Write() Method.

A

WriteLine() method outputs/prints text and adds a new line for each method.

Write() method does not inserts a new line at the end of the output.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are comments used for?

A

In C#, comments are used to add explanatory notes or descriptions within the code. These comments are ignored by the compiler and don’t affect the execution of the program. They help make the code more readable, understandable, and maintainable, especially for others (or yourself in the future) who may be working with the code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the different ways of using a comments?

A

Single comments (//)
MultiLine comments (/* */

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Declare a variable of integer data type, named num and assign a value of 5.

A

int num = 5;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

A variable must be identified with unique names.

The unique names are called identifiers.

What are the general rules to naming a variable?

A
  • Names can contain letters, digits and the underscore character (_)
  • Names must begin with a letter or underscore
  • Names should start with a lowercase letter, and cannot contain whitespace
  • Names are case-sensitive (“myVar” and “myvar” are different variables)
  • Reserved words (like C# keywords, such as int or double) cannot be used as names
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the usage of constant variables?

A

Can NOT overwrite this variable existing value. This means the variable is unchangeable and read-only.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Declare a constants variable and assign a value to it.

A

const int num = 3;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are ways to display a variable?

A

int number = 35;

Console.WriteLine(number);

Console.WriteLine(“Here is the number:” + number);

Console.WriteLine($”Here is the number: {number}”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How would you declare multiple variables?

A

int x = 5, y = 6;

or

int a, b;
a = b = 35;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Describe the meaning of a data type and its importance.

A

Data type specifies the size and type of a variable values.

It is important to use the correct data type for the corresponding variable; to avoid errors, to save time and memory, but it will also make your code more maintainable and readable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Data Types can be categorized into 2 categories, what are they?

A

Value Type and Reference Type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Describe value type data types.

A

Store actual data directly & typically stored in the stack like int, float, double, char, bool, decimal, struct, enum. There are Nullable versions (int? double? etc). Can be stored in heap if part of a reference type.

// These are value types for C#

        // C# Type Keyword          >NET type
        // bool                     System.Boolean
        // byte                     System.Byte
        // sbyte                    System.SByte                 
        // char                     System.Char
        // decimal                  System.Decimal
        // double                   System.Double
        // float                    System.Single
        // int                      System.Int32
        // uint                     System.UInt32
        // nint                     System.IntPtr
        // nuint                    System.UIntePtr
        // long                     System.Int64
        // ulong                    System.UInt64
        // short                    System.Int16
        // ushort                   System.UInt16
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Describe reference type data types.

A

A variable type which instead of storing the value in memory directly, stores the memory location of the actual data. Variable stores the memory reference of the data and not the data directly. Examples are strings, class, Arrays, etc

        // Reference type

        // object                   System.Object
        // string                   System.String
        // dynamic                  System.Object
17
Q

Name the 5 used data types.

A

int, double, string, bool, and char

18
Q

Type casting is when you assign a value of one data type to another type.

What are ways to do this?

A
  • Implicit Casting (automatically) - converting a smaller type to a larger type size
    char -> int -> long -> float -> double

int myInt = 9;
double myDouble = myInt; // Automatic casting: int to double

Console.WriteLine(myInt); // Outputs 9
Console.WriteLine(myDouble); // Outputs 9

  • Explicit Casting (manually) - converting a larger type to a smaller size type
    double -> float -> long -> int -> char

double myDouble = 9.78;
int myInt = (int) myDouble; // Manual casting: double to int

Console.WriteLine(myDouble); // Outputs 9.78
Console.WriteLine(myInt); // Outputs 9

-Type Conversion Methods
Example: Convert.ToString(myInt)

  • Parse Method
    string testing = “2”;
    int numTesting = int.Parse(testing);
19
Q

Explain the TryParse() Method.

A

the TryParse method is used to safely convert a string to a specific data type (such as int, double, DateTime, etc.), without throwing an exception if the conversion fails. This is particularly useful when dealing with user input or data that may not be in the expected format.

Example:
Console.WriteLine(“Enter a number: “);
string? userInput = Console.ReadLine();
int number;
if(Int32.TryParse(userInput, out number))
{
Console.WriteLine(“Continue”);
}
else
{
Console.WriteLine(“Wrong Input”);
}

20
Q

How would you ask a user for a input, lets say a number and print it as output.

A

Console.WriteLine(“Enter a number: “);
int num = Convert.ToInt32(Console.ReadLine());
Console.WriteLine($”Number: {num}”);

21
Q

Operators are used to perform operations on variables and values.

22
Q

Arithmetic Operators are used to perform common mathematical operations.

A

+ Addition x + y
- Subtraction x - y
* Multiplication x * y
/ Division x / y
% Modulus x % y
++ Increment x++
– Decrement x–

23
Q

Assignment Operators are used to assign values to variables.

A

=, +=, -=, *=, /=, %=, &=, |=, ^=,&raquo_space;=, <=

&=, AND - if it exist in both operands
|=, OR - if it exists in either operand
^=, XOR - bit if it is set in one operand but not both
«, Binary Left Shift Operator - The left operands value is moved left by the number of bits specified by the right operand
», Binary Right Shift Operator - The left operands value is moved right by the number of bits specified by the right operand

24
Q

Comparison Operators are used to compare two values or variables. The return value of a comparison is either True or False.

A

== Equal x == y
!= Not Equal x != y
> Greater Than x > y
< Less Than x < y
>= Greater Than or Equal to x >= y
<= Less than or equal to x <= y

25
Q

Logical Operators are used to determine the logic between variable or values.

A

&& - AND - Returns True if both statements are true
|| - OR - Returns True if one of the statements is true
! - NOT - Reverse the result, returns false if the result is true