M2_Baopdf_1

pdf

School

McGill University *

*We aren’t endorsed by this school

Course

206

Subject

Computer Science

Date

Apr 3, 2024

Type

pdf

Pages

11

Uploaded by EarlTree13868

Report
Question 2 1/ 1 point Assume a programmer is using the scanf() statement and would like to read 3 integer numbers from the keyboard. Assume the integer variables are named x, y, and z. Which valid answer(s) should replace CONTENT in scanf(CONTENT); to make the scanf() statement correct? v |'%d %d %d, &x, &y, &z v |"%d %d %d", x,y, z || "%d %d %d", &x, &y, &z v | %d %d %d, x,y, z
Question 3 Consider the following C code: inti=2; intj=4; int *p = &i; int *q = &j; Which of the following are true? v| | Thevalue ofiis 5 | | Thevalue of iis 2 |~ | The value of jis 5 | | Thevalue of jis 4 v/~ Thevalue of iis 3 v| | Thevalue of jis 3 1/ 1 point
Question 4 0/ 1 point Consider the following sequence of statements that are part of a C program function. char a[30]; char *s = "Hello!"; char *n = "Jane"; Which of the following statements, if placed immediately after the above code, are not proper according to C programming conventions and can result in an undefined behaviour? You can assume that the rest of the code is correct, has proper header files, etc. Choose ONLY the correct answers (one or more). | strepy(as); v strepy(a,s); strepy(a,n); = || printf("%s\n", a); » v strcat(a,s); strcat(a,n); 3 [ strepy(a,s); strcat(a,n); w Hide question 4 feedback printf and strcat should not be used before the array a is initialized. The garbage contents of that array's memory location can result in undefined behaviour.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Question 5 0/ 1 point You are on your command-line in a directory with a C file called myProgram.c You type this: echo 5 10 > myProgram.c Which of the following is true: | | This will cause an error message = % | This overwrites the myProgram.c file with 5 10 % [ This redirects 5 and 10 to the standard input of the program v This runs myProgram.c and if it contains the argv parameter in its main function then argv([1] will be equal to 5 w Hide question 5 feedback This is a common mistake that makes students lose their work without being able to recover it. This would be the proper way to redirect arguments: echo 5 10 | ./myProgram where ./myProgram is the compiled version of the program and we are using piping to redirect to standard input of a program instead of redirecting to a file
Question 6 1/ 1 point Consider the following two declarations: intarr[5] = {5,4,32,1}; int *ptr = arr; | | Doing the following after the code in the question: *(arr+4)=5 printf("%d", arr[4]) is not valid C syntax since arr is an array, it means nothing to dereference it || Doing the following after the code in the question: ptr[4]=5; printf("%d", arr[4]) Outputs 5 || Doing the following after the code in the question: *(arr+4)=5 printf("%d", arr[4]) Outputs 5 | | Doing the following after the code in the question: ptr[4]=5; printf("%d", arr[4]) is not valid C syntax since ptr is a pointer
Question 7 What can you say about the following declaration in C in general. Select all that is true. inti=>5; = % || If we follow this declaration by int j = i++, j and i will not be equal | |intis a type that is guaranteed to take 4 bytes % [ Adding the "long" keyword in front of "int" would double its size = || intis a type for which size can vary depending on the implementation | We can follow this declaration by int i = 4; to change the value of i from 5 to 4 Question 8 Given the following struct: struct PERSON { int age; char name[30]; I Which of the following are correct? | scanf("%d", *(p.age)); /| | None of the answers. || scanf("%d", p.age); /| scanf("%d", &(p.age)); 0/ 1 point 1/ 1 point
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Question 9 0/ 1 point Assume that an array has been defined as followed intarr[5] = {5, 3}; Which of the following are true? = || It would have been valid C code to do int arr[] = {5, 3}; instead /| Doing arr[3] has undefined behaviour since we only initialized the first two elements | Thisis invalid code since the number of elements doesn't match the size of the array % (| This is equivalent to int arr[5]; arr[0] = 5; arr[1] = 3; Question 10 0/ 1 point Which of the following are true with respect to the functionality provided by scanf and printf? Choose ONLY the correct options (one or more). || Although printf is not passed the pointer to an integer variable, it can still access the original integer variable's memory location to reac i conter ts. % [~ None of the other options given are true. = x| | When reading an integer, scanf needs to be passed the pointer to an integer variable because it needs to modify the original variable. | scanf's functionality can also be implemented so that it can be given the original integer variable directly without having to pass its address.
Question 11 1/ 1 point What does the following code (only relevant parts shown) do (display)? intx=0; switch(x) { case 0: printf("Hello"); case 1: printf("Bob"); break; default: printf("Wrong value"); Wrong value Hello Bob Hello (e HelloBob Hello Bob w Hide question 11 feedback There is no break after the statements in case O: Also, there is no space printed after case O prints "Hello"
Question 12 0/ 1 point Consider the following array declaration (only relevant parts are shown). long ids[100]; And two pointers long *ptrl = &ids[0]; &ids[5]; long *ptr2 Assuming that long data type is 4 bytes, and pointers occupy 2 bytes, what will be the value of ptr2-ptrl? | | The value will be 20 = % | Thevalue will be 5 % [~ This cannot be calculated /| | The value will be 0 | | The value will be 95
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Question 13 0/ 1 point What can be said about the following C variable declaration statement (assuming struct sd is a valid structure definition). struct sd pl, *p2, p3; Choose ALL and ONLY the correct answers. | | The number of bits required to store p1 will be larger than that required for p2. % [~ P2 and p3 are pointers. v *p2 = &p1 will assign the address of the variable p1 to p2. v/ | Notavalid C variable declaration syntax. = v | Pl is not a pointer. w Hide question 13 feedback Only p2 is a pointer. Correct syntax for assigning address is p2 = &p1. p1 can be potentially smaller in size (imagining have just one char member) compared to p2.
Question 14 Consider the following C program snippet. (Assume rest of the code is correct) char arr[] = {'B', 'o', 'b', '"\0'}; char* str = arr; char* str2 = "Bob"; Which of the following statements are true? Choose ONLY the correct options (one or more). /| | The behaviour of this code is undefined. v/ ifa str) will pass the condition check /|| stremp(arr, str) will return 0. || iflarr==str2) will not pass the condition check | stremp(arr, str) will return 1. w Hide question 14 feedback 1/ 1 point The comparison is between the addresses that either variables are referring. And they are the same. stremp returns O if the contents of the strings/arrays are the same. The comparison is between the addresses that either variables are referring. And they are not the same.