Skip to content

Instantly share code, notes, and snippets.

@rszewczyk
Last active October 19, 2017 18:27
Show Gist options
  • Save rszewczyk/f28da2d1d72c959f621c1bf3fce55a9f to your computer and use it in GitHub Desktop.
Save rszewczyk/f28da2d1d72c959f621c1bf3fce55a9f to your computer and use it in GitHub Desktop.
Practice Midterm

Practice Midterm

I apologize for not posting this over the weekend. Because I ended up not making this available, we will do this as the in class activity for today. You don't need to turn it in. I will provide the answers on Thursday and we will spend some time on any questions you might have then.

The questions on this test are meant to be representative of the type and structure of the questions that you will be asked on the midterm and the range of difficulty - not the content of the questions themselves. The content could be the same as what is covered here but it can also include anything listed in any the objectives up to and including class 12. The midterm will consist of about half as many questions as this practice test. The practice test is harder than the midterm.

It is possible that you will see one or more of these questions, nearly verbatim on the midterm.

On the day of the midterm you will be provided a printed copy of the exam.

#1

Define computer program.













References

class 1 notes

#2

What is one advantage of a high level computer programming language over a low level computer programming language.













References

class 5 notes - slide 3

#3

What is the difference between program syntax and program semantics.













References

class 1 notes

#4

In your home directory on the UMBC Linux server, you have a a directory called cmsc104. In the cmsc104 directory are two directories called class1 and class2.

You have just used the terminal (or PuTTY) to log into the UMBC Linux server. What command would you type to get to the class2 directory?






References

the class 4 assignment

#5

Define variable













References

class 6 notes

#6

Which of the follow are valid variables names in C?

32items radius hello;world printf return theNumber32






References

class 6 notes

#7

Given the following:

#include <stdio.h>  // A refers to this line

int main()
{
  // B refers to this line

  printf("Hello, world!\n"); // C refers to this line

  return 0; // D refers to this line
}

For each of items below, which line from the code above best fits the description

The start of the execution of the program



Exits the program



A function call



Preprocessor directive



References

class 5 notes

#8

Given the following program:

#include <studio.h>

int main()
{
  printf('Hello, world!\n")

  return 0;
}

Identify 3 errors and their corrective action













References

Any program we've written in this class.

#9

Given the following program:

#include <stdio.h>

double area(radius)
{
  return radius * radius * 3.14;
}

int main()
{
  double radius1 = 5.0
  double area = area(double radius1);

  printf("The area of the circle with radius %lf, is %lf", radius1, area);

  return 0;
}

Identify 3 errors and their corrective action













References

The in class assignments for classes 7 and 8

#10

Given the following program:

#include <stdio.h>

int main()
{

  int theNumber10 = 10;
  int theNumber20 = 20;
  int theNumber30 = 30;

  printf("the first number is %d\n", theNumber20);
  printf("the second number is %d\n", theNumber10);
  printf("the third number is %d\n", theNumber30);

  return 0;
}

Write the output below:







References

class 6 notes

class 6 assignment part 2

#11

Given the following program:

#include <stdio.h>

int main()
{

  int theNumber10 = 10;
  int theNumber20 = 20;
  int theNumber30 = 30;

  theNumber10 = 35;

  printf("the first number is %d\n", theNumber20);
  printf("the second number is %d\n", theNumber10);
  printf("the third number is %d\n", theNumber30);

  theNumber30 = 55;

  return 0;
}

Write the output below:







References

class 6 notes

class 6 assignment part 2

#12

Given the following program:

#include <stdio.h>

int main()
{

  int theNumber10 = 10;
  int theNumber20 = 20;
  int theNumber30 = 30;

  {
    int theNumber20 = 100;
    printf("the first number is %d\n", theNumber20);
  }

  {
    int theNumber10 = 0;
  }

  printf("the second number is %d\n", theNumber10);

  {
    theNumber30 = 5;
  }

  printf("the third number is %d\n", theNumber30);

  printf("the fourth number is %d\n", theNumber20);

  return 0;
}

Write the output below:







References

class 6 notes

class 6 assignment part 2

#13

Choose the best answer from the four options.

The three categories of basic data types in C are:

1) double, float, int


2) integers, decimal numbers, characters


3) functions, statements, expressions


4) high level, low level, compiled

References

class 6 notes

class 6 assignment part 2

#14

Define variable initialization













References

class 6 notes

#15

Write a variable declaration that you would use to store the result of calculating the area of triangle where the base is 4.2 (you just need to declare a variable with a valid name of the correct type).




References

class 6 notes

#16

Write a variable declaration that you would use to store the result of calculating the area of triangle where the base is 4.2 (you just need to declare a variable with a valid name of the correct type).




References

class 6 notes

#17

What mathematical operator would you use to divide 4.2 by 5.3 in C (the operator as you would type it in your source code - i.e. the character)?





References

class 6 notes

class 6 assignment part 1

#18

What mathematical operator would you use to calculate the remainder of dividing 7 by 4 in C (the operator as you would type it in your source code - i.e. the character)?





References

class 6 notes

class 6 assignment part 1

#19

Define function arguments













References

class 7 notes

#20

Given the following function definition

    A      B           C 
    ⬇     ⬇          ⬇                     
  double power(double base, double power)
  {
    // D is this section betwen `{` and `}`
  }

For each of items below, which item from the code above best fits the description

A function argument



The function body



The function return type



The function name



References

defining a function from class 7 notes

the sample program from class 7

#21

If I told you that the function from #19 returns the calculation of the first argument (base) raised to the power of the second argument (power), write the code to declare an appropriate variable and store the result of 3 raised to the power of 4 (don't write the whole program, just the part that declares the variable, calls the function and assigns the result).









References

using a function from class 7 notes

the sample program from class 7

#22

What is a benefit of using a function over repetitive logic in your code









References

class 8 notes

#23

Given the following four lines of code:

double theNumber;
printf("Enter a number: ");
printf("1.0 plus %lf is %lf", theNumber, 1.0 + theNumber);
scanf("%d", theNumber);

Identify 3 errors and their corrective actions














References

class 9 assignment

#24

Given the following line of code:

printf("The number is: %lf", 24.65);

Modify it so that it prints 24.65 to three decimal places in a field that is 6 characters wide

References

class 9 assignment

#25

Given the following program:

#include <stdio.h>

int main()
{
  printf("Hello\n");

  if (10 < 12)
  {
    printf("Hello\n");
  }
  else
  {
    printf("Goodbye\n");
  }
}

What is the output?





References

class 12 assignment

#26

Given the following program:

#include <stdio.h>

int main()
{
  printf("Hello\n");

  if (13 < 12)
  {
    printf("Hello\n");
  }
  else if(10 == 11)
  {
    printf("Goodbye\n");
  }
}

What is the output?





References

class 12 assignment

#27

Given the following program:

#include <stdio.h>

int main()
{
  printf("Hello\n");

  if (13 < 12)
  {
    printf("Hello\n");
  }
  else if (10 == 10)
  {
    printf("Goodbye\n");
  }

  if (11 < 12)
  {
    printf("Goodbye\n");
  }
}

What is the output?





References

class 12 assignment

#28

Given the following program:

#include <stdio.h>

int main()
{

  int number1 = 10;
  int number2 = 15;
  int number3 = 0;

  if (number1 > number2)
  {
    number3 = number2 - number1;
    number1 = number2;
  }
  else
  {
    number3 = number1 * number2;
    number2 = number1 + number2;
  }

  if (number1 == number2)
  {
    number3 = number3 / 2;
  }
  else if (number1 < number2)
  {
    number3 = number3 - 5;
  }
  else
  {
    number3 = number3 + number1;
  }

  printf("Number 3 is %d\n", number3);

  return 0;
}

What is the output of the program?




References

class 12 assignment

Answers to the Practice Midterm

#1

Define computer program.

A collection of instructions that performs a specific task when executed by a computer.

#2

What is one advantage of a high level computer programming language over a low level computer programming language.

Any of the following (or similar) answers would be acceptable:

  • The source code for higher level computer programming languages can be the same (or more similar) when written for different types of computers
  • Higher level computer languages are more similar to natural language making them easier to read.
  • Higher level computer languages make the programmer more productive

#3

What is the difference between program syntax and program semantics.

Semantics refers to programmer intent whereas syntax refers to the structure of the source code.

#4

In your home directory on the UMBC Linux server, you have a a directory called cmsc104. In the cmsc104 directory are two directories called class1 and class2.

You have just used the terminal (or PuTTY) to log into the UMBC Linux server. What command would you type to get to the class2 directory?

cd cmsc104/class2

#5

Define variable

A name given to a location in the computer's memory where a programmer can store and/or access data

#6

Which of the follow are valid variables names in C?

32items radius hello;world printf return theNumber32

radius, printf, theNumber32

Note that if I ask a question like this on the test, I will provide you a list of C reserved words.

#7

Given the following:

#include <stdio.h>  // A refers to this line

int main()
{
  // B refers to this line

  printf("Hello, world!\n"); // C refers to this line

  return 0; // D refers to this line
}

For each of items below, which line from the code above best fits the description

The start of the execution of the program: B



Exits the program: D



A function call: C



Preprocessor directive: A

#8

Given the following program:

#include <studio.h>

int main()
{
  printf('Hello, world!\n")

  return 0;
}

Identify 3 errors and their corrective action

  1. Rename studio.h to stdio.h
  2. Change 'Hello, world!\n" to "Hello, world!\n"
  3. Add a semicolon to the end of the line that has the printf call

It would have also been acceptable for you to write out the corect program, e.g.

#include <stdio.h>

int main()
{
  printf("Hello, world!\n");

  return 0;
}

#9

Given the following program:

#include <stdio.h>

double area(radius)
{
  return radius * radius * 3.14;
}

int main()
{
  double radius1 = 5.0
  double area = area(double radius1);

  printf("The area of the circle with radius %lf, is %lf", radius1, area);

  return 0;
}

Identify 3 errors and their corrective action

  1. We can't have both a variable a function with the same name. Rename the area function, to areaOfCircle (in both the definition and the function call)
  2. The argument radius in the function definition is missing its data type - it should be double
  3. When calling the area (or areaOfCircle once its renamed) function, we don't specify the data type of the argument we're passing

It would also have been acceptable to simply write out the correct program, e.g:

#include <stdio.h>

double areaOfCircle(double radius)
{
  return radius * radius * 3.14;
}

int main()
{
  double radius1 = 5.0
  double area = areaOfCircle(radius1);

  printf("The area of the circle with radius %lf, is %lf", radius1, area);

  return 0;
}

#10

Given the following program:

#include <stdio.h>

int main()
{

  int theNumber10 = 10;
  int theNumber20 = 20;
  int theNumber30 = 30;

  printf("the first number is %d\n", theNumber20);
  printf("the second number is %d\n", theNumber10);
  printf("the third number is %d\n", theNumber30);

  return 0;
}

Write the output below:

the first number is 20
the second number is 10
the third number is 30

#11

Given the following program:

#include <stdio.h>

int main()
{

  int theNumber10 = 10;
  int theNumber20 = 20;
  int theNumber30 = 30;

  theNumber10 = 35;

  printf("the first number is %d\n", theNumber20);
  printf("the second number is %d\n", theNumber10);
  printf("the third number is %d\n", theNumber30);

  theNumber30 = 55;

  return 0;
}

Write the output below:

the first number is 20
the second number is 35
the third number is 30

#12

Given the following program:

#include <stdio.h>

int main()
{

  int theNumber10 = 10;
  int theNumber20 = 20;
  int theNumber30 = 30;

  {
    int theNumber20 = 100;
    printf("the first number is %d\n", theNumber20);
  }

  {
    int theNumber10 = 0;
  }

  printf("the second number is %d\n", theNumber10);

  {
    theNumber30 = 5;
  }

  printf("the third number is %d\n", theNumber30);

  printf("the fourth number is %d\n", theNumber20);

  return 0;
}

Write the output below:

the first number is 100
the second number is 10
the third number is 5
the fourth number is 20

#13

Choose the best answer from the four options.

The three categories of basic data types in C are:

1) double, float, int


2) integers, decimal numbers, characters


3) functions, statements, expressions


4) high level, low level, compiled

2

#14

Define variable initialization

Assigning a variable a value for the first time

#15

Write a variable declaration that you would use to store the result of calculating the area of triangle where the base is 4.2 (you just need to declare a variable with a valid name of the correct type).

double baseOfTriangle;

or 

double base;

or

double triangleBase;

etc...

#16

Oops! 16 was a duplicate of 15. It was supposed to be something like:

Write a variable declaration that you would use to store the result of calculating the area of a circle. Assign the variable a value of 10.2.

double areaOfCircle = 10.2;

or 

double areaOfCircle;
areaOfCircle = 10.2;

What mathematical operator would you use to divide 4.2 by 5.3 in C (the operator as you would type it in your source code - i.e. the character)?

/

Define function arguments

The input to the function

#20

Given the following function definition

    A      B           C 
    ⬇     ⬇          ⬇                     
  double power(double base, double power)
  {
    // D is this section betwen `{` and `}`
  }

For each of items below, which item from the code above best fits the description

A function argument: C



The function body: D



The function return type: A



The function name: B

#21

If I told you that the function from #19 returns the calculation of the first argument (base) raised to the power of the second argument (power), write the code to declare an appropriate variable and store the result of 3 raised to the power of 4 (don't write the whole program, just the part that declares the variable, calls the function and assigns the result).

double powerResult = power(3.0, 4.0);

or 

double powerResult;
powerResult = power(3.0, 4.0);

etc

#22

What is a benefit of using a function over repetitive logic in your code

Would accept any of the following (or similar) answers:

  • Using function makes the code more concise and readable
  • Using functions makes your code less error prone

Note - be prepared to answer alternative forms of this question, like "How do functions make your code less error prone?"

#23

Given the following four lines of code:

double theNumber;
printf("Enter a number: ");
printf("1.0 plus %lf is %lf", theNumber, 1.0 + theNumber);
scanf("%d", theNumber);

Identify 3 errors and their corrective actions

  1. printing the result happens before getting the input values. flip the order of the scanf call and the second printf call
  2. the scanf call has the wrong placeholder - it should be %lf
  3. Missing the & before the variable name in the scanf call - it should be &theNumber

You could also just write out the corrected code:

double theNumber;
printf("Enter a number: ");
scanf("%lf", &theNumber);
printf("1.0 plus %lf is %lf", theNumber, 1.0 + theNumber);

#24

Given the following line of code:

printf("The number is: %lf", 24.65);

Modify it so that it prints 24.65 to three decimal places in a field that is 6 characters wide

printf("The number is: %6.3lf", 24.65);

#25

Given the following program:

#include <stdio.h>

int main()
{
  printf("Hello\n");

  if (10 < 12)
  {
    printf("Hello\n");
  }
  else
  {
    printf("Goodbye\n");
  }
}

What is the output?

Hello
Hello

#26

Given the following program:

#include <stdio.h>

int main()
{
  printf("Hello\n");

  if (13 < 12)
  {
    printf("Hello\n");
  }
  else if(10 == 11)
  {
    printf("Goodbye\n");
  }
}

What is the output?

Hello

#27

Given the following program:

#include <stdio.h>

int main()
{
  printf("Hello\n");

  if (13 < 12)
  {
    printf("Hello\n");
  }
  else if (10 == 10)
  {
    printf("Goodbye\n");
  }

  if (11 < 12)
  {
    printf("Goodbye\n");
  }
}

What is the output?

Hello
Goodbye
Goodbye

#28

Given the following program:

#include <stdio.h>

int main()
{

  int number1 = 10;
  int number2 = 15;
  int number3 = 0;

  if (number1 > number2)
  {
    number3 = number2 - number1;
    number1 = number2;
  }
  else
  {
    number3 = number1 * number2;
    number2 = number1 + number2;
  }

  if (number1 == number2)
  {
    number3 = number3 / 2;
  }
  else if (number1 < number2)
  {
    number3 = number3 - 5;
  }
  else
  {
    number3 = number3 + number1;
  }

  printf("Number 3 is %d\n", number3);

  return 0;
}

What is the output of the program?

Number 3 is 145
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment