CSE 240 EXAM #1 REVIEW QUESTIONS WITH
VERIFIED ACCURATE ANSWERS
Lexical Rules - Answers - 1. Identifiers. Names (programmers chosen) for something of
interest (variables, functions, methods, classes, etc.)
2. Keywords. names reserved by the language designer: if, switch, for, int, float, char,
while, etc.
3. Operators. +, *, <, >=, !, &&, ||, etc.
4. Delimiters. , ; () {}
5. Literals. 5, 14.5, 'A', "Hello",
6. Comments. /* ... */, // ...
7. Putting together symbols, to create words
8. Variable names should start with a letter, '$', or '_'.
Syntactic Rules - Answers - * Putting together words, to create sentences
Semantic Rules - Answers - 1. Declaration and Unicity. Reviews for uniqueness and
that the variable has been declared before its usage.
2. Types. Reviews that the types of variables match the values assigned to them.
3. Array's indexes. Reviews that the indexes are integers.
4. Conditions. Reviews that all the expressions on the conditions return a boolean
value.
5. Return Type. Reviews that the value returned by a method matches the type of the
method.
6. Parameters. Reviews that the parameters in a method match in type and number with
the declaration of the method.
Syntactic vs. Semantic - Answers - Syntactic is if the sentence follows the correct
format, semantic is if the sentence makes sense.
*(a + i) = a[i] - Answers - Tricky Examples:
*(a + (i + 1)) != *(a + i) + 1;
/*
This does not shift the letters over to the right by 1.
* e.g. *(a + (i + 1));
It increments the value of i by 1.
Meaning a String of "2020" would become "3131".
*/
*(*(a + i) + j) == a[i][j];
Lexical Errors in C Examples - Answers - int 1dog; // Can't start with anything except for
a letter, '$', or a '_'.
int ch(um; // Using a delimiter.
, char[] x = 'hello there"; // Must have matching quotations and properly closed
quotations.
( ) { } [ ] each one is 1 word: '(' = 1 word and ')' = 1 word.
"==" is one word.
Syntactic Errors in C Examples - Answers - (a1 / a2) = (c3 - c4) // Can't set expressions
equal to each other.
p4rd2 = (x + y) hi (a / z);
struct dog { } // Needs a semicolon afterwards.
if (1} { } // Doesn't match parentheses
Semantic Errors in C Examples - Answers - x = 5; // Valid even if x hasn't been
declared.
while (5) {if (6) { }}
Default Declarations and Sizes of Data Types in C - Answers - int x = signed short int
(int) sizeof(x) = 4 bytes;
char x = unsigned char;
No long or short char, equivalent of byte in Java.
(int) sizeof(c) = 1 byte;
float and double remain the same.
Array Declaration - Answers - char x[] = "abc";
char x[] = {a, b, c}; // Doesn't have null terminator.
char a[5] = {'a', 'b', 'c', '\0'}
char *b = a; // This is valid.
malloc and free Declarations - Answers - You can't overwrite the pointer to the data
within the heap, then be able to free the space as you'd overwrite the reference.
Array Declaration:
int *y = (int*) malloc(sizeof(int) * numElements);
for (int i=0, i<4; i++) {
free(y[i]);
}
free(y);
2D Array Declaration:
int** array = (int**) malloc(sizeof(int*) * rows);
for (int i = 0; i < rows; i++) {
array[i] = (int*) malloc(sizeof(int) * cols);
VERIFIED ACCURATE ANSWERS
Lexical Rules - Answers - 1. Identifiers. Names (programmers chosen) for something of
interest (variables, functions, methods, classes, etc.)
2. Keywords. names reserved by the language designer: if, switch, for, int, float, char,
while, etc.
3. Operators. +, *, <, >=, !, &&, ||, etc.
4. Delimiters. , ; () {}
5. Literals. 5, 14.5, 'A', "Hello",
6. Comments. /* ... */, // ...
7. Putting together symbols, to create words
8. Variable names should start with a letter, '$', or '_'.
Syntactic Rules - Answers - * Putting together words, to create sentences
Semantic Rules - Answers - 1. Declaration and Unicity. Reviews for uniqueness and
that the variable has been declared before its usage.
2. Types. Reviews that the types of variables match the values assigned to them.
3. Array's indexes. Reviews that the indexes are integers.
4. Conditions. Reviews that all the expressions on the conditions return a boolean
value.
5. Return Type. Reviews that the value returned by a method matches the type of the
method.
6. Parameters. Reviews that the parameters in a method match in type and number with
the declaration of the method.
Syntactic vs. Semantic - Answers - Syntactic is if the sentence follows the correct
format, semantic is if the sentence makes sense.
*(a + i) = a[i] - Answers - Tricky Examples:
*(a + (i + 1)) != *(a + i) + 1;
/*
This does not shift the letters over to the right by 1.
* e.g. *(a + (i + 1));
It increments the value of i by 1.
Meaning a String of "2020" would become "3131".
*/
*(*(a + i) + j) == a[i][j];
Lexical Errors in C Examples - Answers - int 1dog; // Can't start with anything except for
a letter, '$', or a '_'.
int ch(um; // Using a delimiter.
, char[] x = 'hello there"; // Must have matching quotations and properly closed
quotations.
( ) { } [ ] each one is 1 word: '(' = 1 word and ')' = 1 word.
"==" is one word.
Syntactic Errors in C Examples - Answers - (a1 / a2) = (c3 - c4) // Can't set expressions
equal to each other.
p4rd2 = (x + y) hi (a / z);
struct dog { } // Needs a semicolon afterwards.
if (1} { } // Doesn't match parentheses
Semantic Errors in C Examples - Answers - x = 5; // Valid even if x hasn't been
declared.
while (5) {if (6) { }}
Default Declarations and Sizes of Data Types in C - Answers - int x = signed short int
(int) sizeof(x) = 4 bytes;
char x = unsigned char;
No long or short char, equivalent of byte in Java.
(int) sizeof(c) = 1 byte;
float and double remain the same.
Array Declaration - Answers - char x[] = "abc";
char x[] = {a, b, c}; // Doesn't have null terminator.
char a[5] = {'a', 'b', 'c', '\0'}
char *b = a; // This is valid.
malloc and free Declarations - Answers - You can't overwrite the pointer to the data
within the heap, then be able to free the space as you'd overwrite the reference.
Array Declaration:
int *y = (int*) malloc(sizeof(int) * numElements);
for (int i=0, i<4; i++) {
free(y[i]);
}
free(y);
2D Array Declaration:
int** array = (int**) malloc(sizeof(int*) * rows);
for (int i = 0; i < rows; i++) {
array[i] = (int*) malloc(sizeof(int) * cols);