CIS 1500 Intro to Programming Final review

0.0(0)
studied byStudied by 0 people
GameKnowt Play
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/95

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

96 Terms

1
New cards

Given below is an incomplete pseudocode written to calculate and print the area of a rectangle, given its length and width. Fill in the blank on step 2 to complete it.
Start of pseudocode
1.Enter the values of length and width of a rectangle.
2.Blank
3.Display the calculated area
4.End

aCalculate area as area = length times width
bCalculate area as area = length + width
cCalculate area as area = length times length
dNone of the given choices

a
Calculate area as area = length times width

2
New cards

The pseudocode given below is written to calculate and display the average of 2 given numbers (note that / is used as the divide operator in programming) . For example, if number1 is given a value of 2, and number2 is given a value of 3, the average of 2 and 3 is 2.5. But step 3 of the pseudocode incorrectly calculates the average due to missing brackets. This is a logical error in the pseudocode. What is the incorrect value produced?

/************ Start of pseduocode ************
Enter a value for the first number (lets call it number1)
Enter a value for the second number (lets call it number2)
Calculate the average as number1 + number2 / 2
Display the value of average
/************ End of pseduocode ************/

a2.5
b3
c3.5
d0

c
3.5

3
New cards

This is a followup question of question 2. The pseudocode given below is (incorrectly) written to calculate and display the average of 2 given numbers (note that / is used as the divide operator in programming).
/***** start of pseudocode*******/
1. Enter a value for the first number ( number 1)
2. Enter a value for the second number (number 2)
3. Calulate average as number 1 + number 2 /2
4. Display value of average
/*********End*********/

Which of the following can be used to replace step 3 so that the average os calculated correctly.
a
Calculate average as (number1 + number2) / 2
b
Calculate average as (number1 + number2 / 2 )
c
Calculate average as number1 + number2

a
Calculate average as (number1 + number2) / 2

4
New cards

int

integer values (positive or negative, but no fractional parts) ex. int numberOfStudents;

5
New cards

char

characters, or single letters. ex. char initial;

6
New cards

float

single precision floating point numbers. ex float average;

7
New cards

double

double precision floating point numbers (bigger -more accuracy). double bigNumber;

8
New cards

Vairiable Naming ; Camel case:

first word starts with lowercase, every other word is Capitalized, e.g. camelCase.

9
New cards

Vairiable Naming ; Pothole case

also called as snake case: every letter is lowercase - multiple words combined with an underscore, e.g. pot_hole

10
New cards

Which of the follong correctly declare a variable in C?
a int aVar;
b integer aVar;
c aVar int;
d char ch;
e x floatingPoint;
f float x;
g float x¸ y;
h float x; float y;
i float x; y;

a int aVar;
d char ch;
f float x;
g float x¸ y;
h float x; float y;

11
New cards

Which on of the following qualify as rules for naming?
a; may consist of letters (upper or lower)¸ digits and the underscore ( _ ) characters.
b; must begin with a letter or an underscore ( _ )
c; must not be longer than 31 characters. Although there is no rule on the length of a variable name¸some compilers may complain if the variable name is more than 31 characters.
d; not be a reserved word of C (e.g. include is a reserved word). Please see section 2.3 for more on reserved words.

a; b; c; d;

12
New cards

Is the following statment true or false:
A constant in C is a variable that is initialized with a value at the time of declaration.

False: A constant is not a variable. A constant must be initialized at the time of declaration and its value cannot change throughout the program - and therefore it is not variable.

13
New cards

#include <stdio.h>
int main(){
float length, width;
float area;

printf("Enter length and width");
scanf("%f%f", &length, &width);
area = length*width ;
printf ("area= %f\n", area);
return 0;
}
Use the formula given on line 14 in Figure 2.5 to match the following values of area.
Drag and drop options on the right-hand side and submit. F

length = 2.5 width = 3.0

length = 0 width = 12.5

length = 2.5 width = 3.0 --> 7.5
length = 0 width = 12.5--> 0.0

14
New cards

If x and y are integers and a and b are float variables, and their values are as shown in the image, then what is the result of the following expressions?

a = 14.0
b = 3.0
x = 14
y = 3

a/b
b/a
x/y
y/x

a / b=4.667
b / a=0.214
x / y=4
y / x=0

15
New cards

Opperator +

Addition; x=a=b; float, double int

16
New cards

Opperator -

Subtration; x=a-b; float, double int

17
New cards

Opperator *

Multiplication; x=a*b; float, double int

18
New cards

Opperator /

Division; x=a/b; float, double int

19
New cards

Opperator %

Modulus; x=a%b; return remainder of two numbers being divided

20
New cards

Opperator ++

Auto-increment; x++; float, double int

21
New cards

Opperator --

Auto-decrement; --x; float, double int

22
New cards

Operator Unary -

Unary minus; x = -y; float, double int;

23
New cards

Precedence of Arthimetic Operators

Unary - unary minus operator has the highest precedence
* \ % have the same precedence, and have a higher precedence than + and -
+ - have the same precedence, and have a lower precedence than *, / and %
Parenthesis () can be used to override this hierarchy.

24
New cards

Evaluate 1/2+ 1/4
a 0
b 3 / 4
c 0.75
d 0.33
e None of the given choices

a 0

25
New cards

Evaluate 23 / 4 + (23.0 / 4.0 - 1)
a 9.75
b 10.5
c 4.75
d None of the given choices

a 9.75

26
New cards

Evaluate the following
13/4.0
13.0/4
13.0/4.0

13/4.0 = 3.25
13.0/4 = 3.25
13.0/4.0 = 3.25

27
New cards

type conversion

conversion of one data type to another, such as an int to a float.

28
New cards

Which datatype best describes the average number of students registered in 3 different sections of a course?
a float
b int
c char
d Any of the given choices

a float

29
New cards

type casting,

allows programmers to explicitly convert the data type of an expression ex.average = (float) (countSection1 + countSection2 + countSection3) / 3;

30
New cards

Evaluate
(float) (13/4)
(float) 13/4
13/(float) 4
13+4.0

(float) (13/4) = 3.0
(float) 13/4 = 3.25
13/(float) 4 = 3.25
13+4.0 = 17.0

31
New cards

Determine the resulting type for each expression. Assume a, b and c are declared as int variables and d is declared as a float variable.

(a * b ) / 2

(a * b) / 2.0

(float) (a * b) / 2

(a / b % c) / d

(a * b) / (int) d

(a * b ) / 2 -->int
(a * b) / 2.0 -->float
(float) (a * b) / 2 -->float
(a / b % c) / d -->float
(a * b) / (int) d-->int

32
New cards

What is printed by printf("%d, z)

int m, n, z;
m = 2;
n = 3;
z = n * m -m/4 - m % 4;

7

33
New cards

How would you write the following variable name in camel case notation: area_rectangle

areaRectangle

34
New cards

What is the result of 1 / 6 + 1 / 12 + 1 / 24?

0

35
New cards

What is the result of the following expression: (float) (1 / 6) + (float) (1 / 12) + (float) (1 / 24)

0.0 - because the numbers being divided are in brackets so the result is calculated first as int then after the answer is type casted as a float

36
New cards

What gets printed by the printf statment
Int main (){
int x;
x=15;
x--;
printf("%d", x)'
return 0;
}

14

37
New cards

The following code throws a compilation error. Which line do you think has this error?
#include <stdio.h>
int main(){
double y = 4.0;
y = y % 2;
printf("%lf\n", y)
return 0;
}

Line 6
y = y % 2;

38
New cards

What does this printf?
int main() {
int c;c = 'd' > 'D';
printf("%d", c);
return 0; }
a 0
b 1
c None of these

b 1

39
New cards

Which of the following is not a relational operator in C?
a =
b <=
c !=
d <
e All of these are relational operators in C

a =

40
New cards

What is output
#include<stdio.h>
int main() {
printf("%d ",!(4>3));
return 0;}
a 0
b 1

a 0

41
New cards

What is output
#include<stdio.h>
int main() {
int v=-1, x=1, y, z;y = !v && x;
z = !v || x;
printf("%d %d %d %d", v, x, y, z);
return 0;
}
a -1 1 0 1
b -1 1 1 1
c -1 1 0 0
d -1 1 1 0
e None of these

a -1 1 0 1

42
New cards

What is output
#include<stdio.h>
int main() {
printf("%d ",4>3 && 11<12);
return 0;
}
a 0
b 1
c None of these

b 1

43
New cards

What is output
#include<stdio.h>
int main() {
printf("%d ",4>3 || 11>12);
return 0;
}
a 0
b 1
c None of these

b 1

44
New cards

What is output
#include<stdio.h>
int main() {
printf("%d ",4<3 || 11>12);
return 0;
}
a 0
b 1

a 0

45
New cards

What is output
#include <stdio.h>
int main() {
int a = 3, b = 4, c = 2;
if ((a+b)/c == (b+c)/c) {
printf("Wonderful \n");
}
return 0;
}
a Wonderful
b 0
c None of these

a Wonderful

46
New cards

What is output
#include <stdio.h>
int main() {
int x = 3, y = 4;
if( x < 5 && y < x )
printf(" x is less than 5, but greater than Y");
else { 7. if( x < 5)
printf( "x is less than 5, and not greater than y" );
else {
if( y < x )
printf( "x is not less than 5 and is greater than y" );
else
printf( "x is not less than 5 and not greater than y" );
}
}
a x is less than 5, but greater than y
b x is less than 5, and not greater than y
c x is not less than 5 and is greater than y
d x is not less than 5 and not greater than y
e None of these

b x is less than 5, and not greater than y
e None of these

47
New cards

Is the logic of the program correct
#include <stdio.h>
int main() {
int x = 3, y = 4;
if( x < 5 && y < x )
printf( "x is less than 5, but greater than y" );
else {
if( x < 5)
printf( "x is less than 5, and not greater than y" );
else {
if( y < x )
printf( "x is not less than 5 and is greater than y" );
else
printf( "x is not less than 5 and not greater than y" );
}
}

the logic of the program is correct

48
New cards

What is output
#include <stdio.h>
int main() {
int x=4;
switch(x) {
case 0: printf("0 ");
case 2: printf("2 ");
case 4: printf("4 ");
default: printf("SCARY ");
}
x=5;
switch(x) {
case 0: printf("0 ");
case 2: printf("2 ");
case 4: printf("4 ");
default: printf("DOGS ");
break;
}
return 0;
}

a 4 DOGS
b 4 SCARY DOGS
c 2 4 SCARY DOGS
d 0 2 4 SCARY 0 2 4 DOGS
e None of these

b 4 SCARY DOGS

49
New cards

What is output
#include <stdio.h>
int main() {
int x=2;
switch(x) {
case 0: printf("0 ");
case 2: printf("2 ");
case 4: printf("4 ");
default: printf("SCARY ");
}
x=5;
switch(x) {
case 0: printf("0 ");
case 2: printf("2 ");
case 4: printf("4 ");
default: printf("DOGS ");
break;
}
return 0;
}
a 4 DOGS
b 4 SCARY DOGS
c 2 4 SCARY DOGS
d 0 2 4 SCARY 0 2 4 DOGS
e None of these

c 2 4 SCARY DOGS

50
New cards

What is output
#include <stdio.h>
int main() {
int a;
switch(a) {
printf("Calgary is ");
}
printf("Fantastic");
}
a Calgary is Fantastic
b There would be no output
c Fantastic
d Compiler Error
e None of these

c Fantastic

51
New cards

What is output
#include <stdio.h>
int main() {
int a = 3, b = 4, y;
y = (++a==b? a : a+b );
printf("%d", y);
return 0;
}

4

52
New cards

Logical Opperators

&& And
|| Or
! Not

53
New cards

Precendence of relational vs logical operators

logical operators have lower precedence as the relational operators,

54
New cards

Given x, a, b, c and d are integers.a = 3, b = 10, c = -34, d = 8;What is assigned to x in each of the following cases?For example, x = a < 3; assigns 0 to x.

x = b >= c

x = d == 8;

x = d = 3;

x = d = 0;

x = b >= c --> 1
x = d == 8; --> 1
x = d = 3; --> 3
x = d = 0;--> 0

55
New cards

What is printed if d = 8
if (d=3) {
printf("Really!!\n");
} else {
printf("Catch\n");
}
a Really!!
b Catch
c Really!! \n
d Catch \n
e None of the given choices

a Really!!

56
New cards

What gets printed if s is 80?
if (s>=90){
printf("A");
}else if (s>=70) {
printf("C");
} else if (s>=80){
printf("B");
}else {
printf("D");
}

C

57
New cards

How many iterations does this while loop make>
int i =1;
while (i<5){
printf("*");
i = i+1;
}

4

58
New cards

What does this while loop print?
int i =1;
while (i<5){
printf("*");
i = i+1;
}

**** (because there are no newlines in the print statement)

59
New cards

How many iterations does this while loop make?
int i =1;
while (i<5){
i = 1;
printf("*");
i = i + 1;
}

Infinite because i is redefined as 1 for each loop.

60
New cards

How many iterations does this while loop make?
int i =1;
while (i=5){
printf("*");
i = i + 1;
}

Infinite

61
New cards

How many iterations does this while loop make?
int i =1;
while (i==5){
printf("*");
i = i + 1;
}

0

62
New cards

What is output of following program ?
#include <stdio.h>
int main (){
for (int i =0; i < 3; i++){
printf("%d", i);
}
return 0;
}

0 1 2

63
New cards

What is output of following program?
#include <stdio.h>
int main (){
int j, i, count;
count =0;
for (i=0; i<4; i++){
for(j=0; j<3, j++){
count ++;
}
}
printf("%d", count);
return 0;
)

12

64
New cards

What is output of following program?
#include <stdio.h>
int main (){
int j, i, count;
count =0;
for (i=0; i<4; i++){
for(j=0; j<3, j++){
count ++;
}
}
printf("%d %d", i, j );
return 0;
)

4 3

65
New cards

Fill in the blanks in the statement using the given code: This code has <blank1> function calls and <blank2> user-defined function definition (s).

/ function prototypes /
int sum(int a, int b);

int main(void)
int x, y, z;
× = 5; y = 3;
z = sum(x, y) ;
printf ("Sum of %d and %d = %d\n", x, y, z);
printf ("Sum of %d and %d = %d\n", x, y, sum (x, x));
return (0);
/*
* Compute sum of two integers.
* Given a and b
*/
int sum(int a, int b)
{
return (a + b);
}

This code has 2 function calls and 1 user-defined function definition (s).

66
New cards

#include <stdio.h>
int main(void)
int x, y, z;
× = 5; y = 3;
z = sum(x, y) ;
printf ("Sum of %d and %d = %d\n", x, y, z);
printf ("Sum of %d and %d = %d\n", x, y, sum (x, x));
return (0);
/*
* Compute sum of two integers.
* Given a and b
*/
int sum(int a, int b)
{
return (a + b);
}

Which line or lines make a function call?

Which line or lines is a function header?

Which line or lines is a function prototype?

List the function parameters of sum?

How many arguments does sum have in its call on line 13?

How many arguments does sum have in its call on line 18?

Which line or lines make a function call? - Lines 13 and 18
Which line or lines is a function header?- Line 26
Which line or lines is a function prototype?- Line 5
List the function parameters of sum?- a and b
How many arguments does sum have in its call on line 13? - 2
How many arguments does sum have in its call on line 18? - 2

67
New cards

Where can function definitions be placed?

after the return 0;
}

68
New cards

What gets printed by the following code:
#include<stdio.h>
void fun (int);
int main (){
int z=5;
fun (z);
printf("In main, z= %d", z);
return 0;
}
void fun (int z){
z = z+2;
printf("In fun. z = %d", z);
}
a In fun, z = 7 In main, z = 5
b In main, z = 5 In fun, z = 7
c In main, z = 7 In fun, z = 5
d In fun, z = 5 In main, z = 5
e none of the given choices

a In fun, z = 7 In main, z = 5

69
New cards

#include<stdio.h>
int main (){
int aNum = 15;
int * ptrInt;
ptrInt = &aNum
aNum = aNum + 5;
printf("%d \n", *ptrInt);
return 0;
}
a 20
b 15
c 0x7ffee3545a2c
d 0
e none of the given choices

a 20

70
New cards

Complete the function definition of swap by filling in the blanks. Function swap interchanges the 2 values it takes as input. For example, if swap takes num1 as 10 and num2 as 15, then it makes num1 as 15 and num1 as 10. Note that this change must also be reflected in the corresponding arguments.
void swap (int num1, int num2) {
int temp;
temp = *num1;

blank1 -

blank2 -

}

num1 = num2;
num1=num2;
or
*num2 = temp;
*num2=temp;

71
New cards

When a called function completes its task, it normally _________
Select an answer and submit. For keyboard navigation, use the up/down arrow keys to select an answer.
a terminates program execution normally
b aborts program execution
c logs its results
d returns to the calling function
e none of the given choices

d returns to the calling function

72
New cards

Given the following function definition, which of the following is a valid prototype of increment? int increment (int num) { num++; return num;}
Select an answer and submit. For keyboard navigation, use the up/down arrow keys to select an answer.
a increment (10);
b x = increment (10);
c int increment (int);
d increment (num);
e none of the given choices

c int increment (int);

73
New cards

FFill in the blanks with True / False **In
1. Arguments can be constant values - blank1 -
2. Arguments can be variables - blank2 - 3. Arguments can be expressions - blank3 -
4. Parameters can be constant values - blank4 -
5. Parameters can be variables - blank5 - 6. Parameters can be expressions - blank6 -

1. True 2. True 3. True 4. False 5. True 6. False

74
New cards

Given
int firstList ​[4] = {33, 44, 55, 66};
int secondList ​[4] = {0, 0, 0, 0};
True or False:
firstList = secondList copies 0s into each firstList element.

False

75
New cards

What is the output of C Program.?#include <stdio.h>
int main() {
int a​[] = {3, 1,2, 4};
int b​[4] = {8, 6,7,9};
printf("%d, %d", a​[1], b​[1]);
return 0;}

a 3, 8
b Compiler error
c 1, 6
d 0, 0
e None of these

c 1, 6

76
New cards

What would be printed?
float x​[7] = { 23.0, -28.6, 30.2, 29.0, -8.0, -30.0, 27.8 };
int i = 2;printf ("%.2f \n ", x​[i]);
a 30.2
b-28.6
c 30.20
d -28.60
e None of these

c 30.20

77
New cards

What is printed when the following code segment is run within a program?
float x​[7] = { 23.0, -28.6, 30.2, 29.0, -8.0, -30.0, 27.8 };
int i = 3;
printf ("%.2f \n ", x​[i++] + 2);
a 32.20
b 31.00
c -6.00
d 29.00
e None of these

b 31.00
i++ increments i by one AFTER the current operation is completed. So x​[i++] returns the element at position 3, then add two to the value of the element (29.0 + 2).

78
New cards

What is printed?
float x​[7] = { 23.0, -28.6, 30.2, 29.0, -8.0, -30.0, 27.8 };
int i = 3;
printf (" %.2f \n ", x​[++i] + 3);
a 32.00
b 33.20
c -5.00
d 29.00

c -5.00
++i would increment i by one BEFORE the operation and would returns the element at position 4.

79
New cards

What is printed?
float x​[7] = { 23.0, -28.6, 30.2, 29.0, -8.0, -30.0, 27.8 };
int i = 3;
printf (" %.2f \n ", x​[i + 1]);
a 29.00
b -8.00
c -30.00
d 30.20
e None of these

b -8.00

80
New cards

What is printed?
float x​[7] = { 23.0, -28.6, 30.2, 29.0, -8.0, -30.0, 27.8 };
int i = 2;
printf (" %.2f \n ", x​[3*i - 1]);
a 30.00
b -30.00
c -31.00
d -29.00
e None of these

b -30.00

81
New cards

Trace the code given below and show what gets printed?
char name ​[30] = "Andre DeGrass";
printf ("%d \n", strlen (name));

13

82
New cards

Trace the given code and show what gets printed?
char name​[30] = "Andre DeGrass";
name​[3] = '\0';
printf ("Name is %s\n", name);
a Name is And
b Name is Andre
c Name is Andre DeGrasse
d Name is %s\n
e None of the given choices

a Name is And

83
New cards

Trace the given code and show wheat gets printed?
char name ​[30] = "Harry Potter";
char name2 ​[10] = "Ron";
strcpy (name, name2);
printf ("%s\n", name);
a Ron
b RonryPotter
c Ron\0
d Ron Potter
e None of the given choices

a Ron

84
New cards

In C a pointer variable to an integer can be created by the declaration:
a int p*;
b int *p;
c int $p;
d None of these

b int* p;

85
New cards

What is the output of the following C code?
void main() {
int a [] = {1,2,3,4,5}, *p;
p=a;
++*p;
printf("%d", *p);
p +=2;
printf("%d", *p);
}
a 24
b 23
c 34
d 22

b 23
Note that since p is a pointer, then p = a assigns p to the address of the 1st element of a - which is 1. In the first part ++*p; -- add 1 to this first element.p += 2 adds two to the pointer p, thus p points to the third element, which is 3.

86
New cards

The function of the "w+" mode is to create a text file for

a writing, and discard the previous contents if any
b update, and discard the previous contents if any
c writing, while keeping the previous contents if any
d update, while keeping the previous contents if any

a writing, and discard the previous contents if any

87
New cards

Which of the following is a correct use of the fopen() function?
a fopen(.//File8.txt, r);
b fopen(".//File8.txt", r);
c fopen(.//File8.txt, "r");
d fopen(".//File8.txt", "r");

d fopen(".//File8.txt", "r");

88
New cards

If an error occurs while opening a file in the read mode, function fopen returns ________.

a NULL
b 0
c -1
d 100
e None of the given choices

a NULL

89
New cards

Which mode would you use if you wanted to open a file for writing?

a w
b r
c fscanf ()
d fopen ()
e None of the given choices

a w

90
New cards

Which function may be used to read a single character from a file at a time?

a fscan()
b fscanf()
c fgetc()
d fgets()

c fgetc()

91
New cards

Which function which is used as a formatted output file function.
a printf()
b fprintf()
c fputs()
d fputsf()

b fprintf()

92
New cards

How would the string "ACE" be translated to it's ASCII Hexadecimal code:
a 41 43 45
b 61 63 65
c 51 53 55
d 41 45 43

a 41 43 45

93
New cards

Structures may contain variables of many different data types in contrast to ___________ that can contain only elements of the same data type.

a files
b arrays
c functions
d constants
e None of the given choices

b arrays

94
New cards

How would you describe movies in the given code?
struct Movie{
int mid;
char title [30];
};
typedef struct Movie movies;
a variable of type struct
b variable of type struct Movie
c new name for struct Movie
d variable of type array
e None of the given choices

c new name for struct Movie

95
New cards

Which of the following statement correctly prints aStudent's name? Use the same struct definition given earlier in this section (also given below for convenience.
typedef struct Student {
char name [30];
int id;
double assignmentMarks [3];
double finalExam;
} students1500;
a printf ("%s \n", aStudent.name);
b printf ("%s \n", name);
c printf ("%c \n", aStudent.name);
d printf ("%s \n", aStudent.name ​[30]);

a printf ("%s \n", aStudent.name);

96
New cards

typedef struct Card {
char symbol ​[20];
char colour ​[20];
int number;
} cards;
cards deck ​[52];

What is the datatype of deck?

What is the datatype of the symbol of the first deck?

Write a statement that assigns 14 to the first deck's number?

Write a statement that assigns "Hearts" as the first deck's symbol?

Write a statement that stores 'R' as the first character of the first deck's colour?

What is the datatype of deck? --> Array
What is the datatype of the symbol of the first deck? --> String
Write a statement that assigns 14 to the first deck's number?-->deck​[0].number = 14;
Write a statement that assigns "Hearts" as the first deck's symbol? --> strcpy (deck[0].symbol,"Hearts");
Write a statement that stores 'R' as the first character of the first deck's colour? --> deck​[0].colour​[0] = 'R';