Final Exam Practice 1. How many elements are in the Array B: int B[5][3] The answer is 15. Because B has 5 rows and 3 columns 2. The following C code is executed on the Arduino and suppose to print Hello, but instead of printing Hello, it prints a random number. How could we fix the problem? char MyString[8] = 'Hello'; Serial.println( MyString ); MyString[8] should be declared as char MyString[8] = "Hello"; 3. When a variable is created in C, a memory address is assigned to the variable. True 4. Which format specifier is often used to print integers? a. %f b. %d c. %c d. %s 5. In the C programming language. If you declare a function as int then that function must return a value. True 6. You are required to declare an array variable of any dimension to hold 100 integer numbers, which one(s) of these declarations are correct. Choose all that apply Note: For this question will be graded as "right minus wrong, for example if you chose one correct answer and one wrong answer, your score for this question will be zero int C[100] ; int C[]100 ; int C[]=100; int C[10][10]; int C[5][20]; 7. It is required to write a code to make the Arduino wait for 1 second (implement a delay of one second without executing any other instructions). Choose All that applies Note: For this question will be graded as "right minus wrong" , for example if you chose one correct answer and one wrong answer, your score for this question will be zero a. delay(1000); This is correct. Because the unit of delay function is ms and 1000ms = 1 sec b. delay(1); c. delayMicroseconds(1000); d. delayMicroseconds(1000000) ; This is also correct. Because the unit is micro second and 1000000*0.000001 = 1 sec e. int i; for (i= 0; i<10;i++){ delay(100); } This is correct too. Because on each iteration it waits for 0.1 sec and it iterates through the loop for 10 times. So 10*0.1 = 1 sec f. delay(500); delay(500); This is also true. Because it waits for 500 ms twice: 500+500= 1000 ms = 1 sec g. int i; for (i= 0; i<20;i++){ if( i % 2 == 0) { delay(100); } } This is also true. Because although the loop counter is supposed to start from 0 and ends at 19 (0<= i<20), but there is another condition inside the loop that says only if (i%2 == 0), the body of the for loop will be executed! (let’s see what i%2==0 mean? This expression evaluates the remainder of the division of i to 2? And when i%2 equals to 0 , it means i is an even number.) Since we only have 10 even numbers between 0 and 19 which are: 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, the loop iterates only 10 times and, in each iteration, it will wait 100 ms. So, 100*10 = 1000 ms = 1 sec. 8. What is the output range of numbers from an Analogue input on the Arduino Mega 2560? The answer is : 0 – 1023 (It starts from 0 not 1) The Mega 2560 has 16 analog inputs, each of which provide 10 bits of resolution (i.e. 1024 different values) 9. Arduino IDE consists of 2 functions. What are they? a. Loop() and build() and setup() b. Build() and loop() c. Setup() and build() d. Setup() and loop() Setup() is called once in the program when a sketch starts after power-up. It is used to initialize variables, input and output pin modes, and other libraries needed in the sketch. Loop() is used after setup() been called, function loop() is executed repeatedly in the main program. It controls the board until the board is powered off or is reset. 10. What do we call the following? int myNumbers[] = {25, 50, 75, 100}; a. A pointer b. A function c. An array d. A class 11. If the C code below is executed, how many times does the statement "x = y" executes? Answer: 45 times 12. Array index starts with 1. False because Array index starts with 0 14. What is the output of the following program? Answer: z = 0, w =1 15. How many analog pins are used in Arduino Mega board? a. 12 b.16 c. 8 d.14 16. How many times will the following loop execute? for(j = 1; j <= 10; j--) b. Forever b. Never c. 0 d.1 20. Which one of the following is a loop construct that will always be executed once? a. For b. while c. switch d. do while The body of a loop is often executed at least once during the do-while loop. Once the body is performed, the condition is tested. If the condition is valid, it will execute the body of a loop; otherwise, control is transferred out of the loop. 21. What will be the result of num variable after execution of the following statements? int num = 58; num %= 11; a. 3 b. 5 c. 8 d. 11 num %= 11 (this means calculate the value of (num % 11) and assign it again to the variable num, since num is equal to 58, so num updates to 58 % 11 which is the remainder of 58/11 which equals to 3) 22. Which of the statements are correct about 5 used in the program? int num[5]; num[5]=20; a. In the first statement 5 specifies an array size, whereas in the second statement it specifies a particular element of array. b. In the first statement 5 specifies a particular element, whereas in the second statement it specifies a array size. c. In the first statement 5 specifies a particular element, whereas in the second statement it specifies a type. d. In both the statement 5 specifies array size. 24. If a variable is declared inside a function, what kind of variable is this? a. global variable b. local variable c. extended variable d. None of the above 25. A………………….. statement is often used in a menu driven program where a user would make a selection according to their needs. a.For b. if – else c. switch d. do-while 26. The default parameter passing mechanism is a. Call by value b. call by reference c. call by value result d. None 31. A function prototype is the declaration of a function that specifies function's name, parameters and return type and the body. False A function prototype gives information to the compiler that the function may later be used in the program. It doesn't contain function body. Example: int addNumbers(int a, int b); name of the function is addNumbers() return type of the function is int two arguments of type int are passed to the function Note: The function prototype is not needed if the user-defined function is defined before the main() function. 32. What is the output of the following code: int myFunction(int x, int y) { return x + y; } int main() { printf("Result is: %d", myFunction(5, 3)); return 0; } a. Result is: 5+3 b. Result is: 8 33. you must include the ……… c. Compilation error d. 0 header file in your program: To execute the following line: double num = sqrt(16)); a.<stdlib.h> b. <stdio.h> c. <math.h> d. <iomanip.h> 34. What is the correct declaration of the function display() if the last statement in the function is : printf ("%d", myInt); a. string display(); b. void display(); c. char display(); d. print display(); void display(); is the correct answer as the function does not return any value and it just prints some info on the screen. 35. What would be printed at the screen after executing the following statement: printf("%d", 10 > 9); a.10 b.1 c.0 d. Compilation error The statement 10>9 evaluates to true so a digit (%d) that will be displayed on the screen is 1 (true) because 10 is greater than 9 36. You are able to change the size of the array after creation. False You can’t change the size of the array after creation. 37. What is the output of this program? #include <stdio.h> int main() { int a=10; if(a=5) printf("YES"); else printf("NO"); } a. YES b. No c. Error d. None of the above Note: if (a==5) , evaluates if a is equal to 5 and returns true or false. If (a=5), assigns 5 to variabl e a and a is now equal to 5. So basically if (a=5), just sets the value of a to 5 and it evaluates to t rue because there is no condition in the if statement (there is an assignment which assigns 5 to a but that is not considered as a condition). When a condition is missing in in a C program, it is a ssumed to be present and taken to the true and the if statement will be executed. 38. How many times the loop will execute? for(int i = 0 ; i < 10 ; i++) { i = i*2; i--; } a.10 b. 5 c. 0 d. Infinite The value of i is always less than 1. 41. What is the output of this program? #include <stdio.h> int main() { int x = 1, y = 2; printf("%d", x, y); return 0; } a. 1 b. 2 c. Compilation Error d. 1, 2 Since only one %d exists in the printf(), it only displays the first integer value which is x.