Week 3 Flashcards

(59 cards)

1
Q

Describe a conditional statement with a “plan B”.

A

if (true_or_false_condition)

perform_if_condition_true;

else

perform_if_condition_false;

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

Code a conditional statement to the following scenario: If the weather is good, go to the park. If the weather is bad, stay at home.

A

if (weather_is_good) GoToPark();
else
StayAtHome();

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

For an if-else statement if you want to give more than one instruction how would you code that?

A
  • group instructions in a block with {}

if (condition) {instruction1();
instruction2();}
else {instruction1();
instruction2();}

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

Condition statements can be …, which means more than one condition can be included in a statement. The else statement will refer to the … if statement. … improves readability and emphasizes the nesting of inner conditional statements.

A

nested, closest former, indentation

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

Code the following:

If the weather is fine, we’ll go for a walk. If we find a nice restaurant, we’ll have lunch there. Otherwise, we’ll eat a sandwich. If the weather is poor, we’ll go to the theater. If there are no tickets, we’ll go shopping in the nearest mall.

A
if (Weather_good)
    if (Nice_restaurant_found)
       Have_lunch();
    else
       Eat_sandwhich();
else
    if(Tickets_available)
        Go_to_cinema();
    else
        Go_shopping();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a cascade?

A

Nested conditional statements that have several alternatives if a condition is not met.

if (condition) instruction();
   else if (nested condition 1)
              instruction();
   else if (nested condition 2)
              instruction();
else instruction();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Most computers store ints using … bits (… bytes).

A

32, 4

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

What issues arise when using ints? (3)

A
  • we might not need such a large value using 32 bits for simple calculations such as e.g. counting sheep
  • we might need even larger values where 32 bits are not enough
  • if we don’t need negative numbers why waste half the bits on this
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Can we change the range of ints the computer actually uses?

A

Yes, you can change the memory requirements allocated to storing ints. You can also specify if you only want to use positive numbers.

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

What does “long” do?

A
  • declares we want to use a greater range than 32 bits for ints
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does “short” do?

A
  • declares we want to use a smaller range than 32 bits for ints
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does “unsigned” do?

A
  • specifies that we only want to use non-negative ints
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you use unsigned, long and short?

A

short int Counter
unsigned int Counter
long long int Counter

  • int can be omitted as the use of the other 3 already implies we are dealing with int

short Counter
unsigned Counter
long long Counter

  • modifiers can also be mixed together

unsigned long long Counter

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

Which restrictions exist regarding the use of the modifiers short, long and unsigned?

A
  • only unsigned can be used in conjunction with char
  • short and long can’t be used simultaneously
  • short and unsigned can’t be used with float, long can
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is long float a synonym for?

A

double

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

What is the difference between long float and double?

A
  • long float stores 32 bits (8 digits), double 64 (15-17 digits)
  • double is more accurate
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is numerical anomaly?

A
  • float and double have finite storage capacity, only a number of digits can be displayed
  • if you add a very small number to a very large number, the small one will disappear
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is conversion?

A

~ changing the type of the data (perhaps combined with a change of its value, which may be caused by a loss of accuracy)

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

What 2 types of conversions does the C language know?

A

implicit and explicit conversions

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

What is an implicit conversion?

A

~ work according to language rules and are not specified in the code in any visible way; their operation is silent and automatic

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

What is an explicit conversion?

A

~ are carried out at the developer’s request; the developer should insert them explicitly inside the code indicating which value should be converted and into which resulting type

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

Implicit conversion happens according to which rules?

A

Rules apply in order of following strict rules until all the data in the expression has the same type:

1) data of type char or short int will be converted to type int (integer promotion)
2) if there is any value of type float, the other data will be converted to float
3) if there’s any value of type double, the other data will be converted to double
4) if there’s any value of type long int, the other data will be converted to long int
5) last conversion as requested by context

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

How do you introduce explicit conversions?

A
  • with the typecast operator

(type) value

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

How would you convert the following? (implicit conversion)

int Int
char Char
short Short
float Float

Int = Short + Char + Float

A

1) promotions first:
Int = (int)Short + (int)Char + Float

2) Int = (float)((int)Short + (int)Char) + Float)
3) There is no double.
4) Due to the context of the assignment to Int, all need to be converted to int.

Int = (int)((float)((int)Short + (int)Char) + Float));

25
Code a program that finds the larger of 2 numbers.
int number1,number2; int max; /* larger number */ scanf("%d",&number1); scanf("%d",&number2); max = number1; /* assumption */ if(number2>max) /* is it true? */ max = number2; printf("The largest number is %d \n",max);
26
What is pseudo-code?
- it isn’t a programming language at all (it can be neither compiled nor executed), but it is formalized
27
What does a loop do?
- similar to an if statement, but the execution gets repeated as long as the condition is still true while(conditional_expression) statement;
28
An instruction or instructions executed inside the loop are called the loop's .... If the condition is “false” (equal to zero) as early as when it’s tested for the first time, the body is ... The body should be able to change the ..., because if the condition is true at the beginning, the body might run continuously to ...
body, not executed even once, condition value, infinity
29
How do you execute more than one statement in a loop?
- with a block {} ``` while(conditional_expression) { statement1; statement2; : . statementn; } ```
30
What does this expression do? while(1){ printf("He is a cute dog!"); }
- as the condition is always true (1), this statement will be printed infinitely
31
What does "int variable = 0" do?
- declares a variable called "variable" at the same time as declaring its value "0"
32
The part of the declaration placed on the right side of = is called...
... initiator.
33
Code a program which counts odd and even numbers and terminates when a 0 is encountered.
int Evens = 0; int Odds = 0; int Number; scanf("%d", &Number); while (Number != 0) { if(Number % 2) Odds++; else Evens++; scanf("%d", &Number); } printf("Even numbers: %d\n",Evens); printf("Odd numbers: %d\n",Odds);
34
How would you code the condition that counts that a number is odd?
if(Number%2==1)
35
What does a "do loop" do?
- similar to a loop, but the instructions run at least once even if the conditions are not met - essentially the conditions are checked at the end of body execution (ordinary loop checks at the beginning)
36
Give the simplified syntax of a do loop.
/* simple */ do statement; while(condition); ``` /* several instructions in a block */ do { statement1; statement2; : . statementn; } while(condition); ```
37
How does a "for loop" distinguish itsself from other loops?
- counts the turns of the loop rather than checking the condition - has an initiation (of counter), a checking (of conditions) and a modifying (of counter) phase in one for(i=100;i<100;i++); { }
38
The variable used for counting the loop turns of a for loop is often called a ... The for loop has an interesting ... If we omit any of its three components, it’s presumed that there is a ... there instead.
control variable, singularity, 1
39
Code a program whose task is to write some of the powers of 2.
int exp; int pow=1; for(exp=0;exp<16;exp++){ printf("2 to the power of %d is %d\n",exp,pow); pow*=2; }
40
C provides 2 special instructions to amend for loops, what are they?
break and continue
41
What does the break special instruction do?
- exits the loop immediately and unconditionally ends the loop’s operation - program begins to execute the nearest instruction after the loop's body
42
What does the continue special instruction do?
- behaves as if the program has suddenly reached the end of the body - the end of the loop’s body is reached, the control variable is modified, and the condition expression is tested
43
Write a short program that searches for the largest number using the fo loop.
int number int max=-100000 int counter=0 ``` do{ scanf("%d",&and number); if(number!=-1) counter++ if(number>max) max=number; } ```
44
What is a conjunction in the context of C?
- 2 or more conditions are combined to give way to a certain instruction - e.g. if the weather is good AND you are free, we can go to the festival
45
What is a disjuction in the context of C?
- the execution of the instruction depends on the fulfilment of at least one of 2 or more conditions - e.g. if I win the lottery OR inherit a fortune, I'll buy a private plane
46
OR and AND are ... operators.
logical
47
What does the conjunction operator look like?
&& | - it's called the conjunction digraph ampersand ampersand
48
What does the disjunction operator look like?
|| | - it's called disjunction digraph bar bar
49
What is the operator turning a true condition into a false condition and vice versa?
- the unary operator of logical negation "!"
50
What is "!(Variable <= 0)" equivalent to?
Variable >0
51
What is "!(Variable == 0)" equivalent to?
Variable != 0
52
What does De Morgan's Law state?
The negation of a conjunction is the disjunction of the negations. The negation of a disjunction is the conjunction of the negations.
53
How would you code De Morgan's Law?
!(A && B) ==
54
Logical operators take their arguments as a ..., regardless of how many bits they contain. The operators are aware only of the value: ... (when all the bits are reset) means “false”; ... (when at least one bit is set) means “true”. The result of their operations is one of the values: ...
whole, 0, not 0, 0 or 1
55
What are bitwise operators? List them.
- they allow to manipulate single bits of data - they are: & (ampersand) as a bitwise conjunctio | (bar) as a bitwise disjunction ~ (tilde) as a bitwise negation ^ (caret) as a bitwise exclusive or
56
What is the difference between logical and bit operators?
- logical operators do not penetrate into the bit level of its argument (only interested in the final integer value) - bitwise operators deal with every bit separately (e.g. if int variable occupies 32 bits, bitwise operation will be 32-fold evaluation of the logical operator) - bitwise operators do not work with float
57
What is the abbreviated form of conjunction, disjunction and exclusive or?
``` x = x & y; x &= y; x = x | y; x |= y; x = x ^ y; x ^= y; ```
58
A sequence of zeros and ones whose task is to grab the value or to change the selected bits is called a ...
bitmask
59
What kind of tasks can you carry out with bits? (5)
- check the state of a bit - reset your bit - set your bit - negate your bit - shift your bit