Test1CSE130 ethan suttor 5519519

docx

School

University of Louisville *

*We aren’t endorsed by this school

Course

130

Subject

Computer Science

Date

Dec 6, 2023

Type

docx

Pages

8

Uploaded by MinisterBraveryFalcon39

Report
CSE130 - Dr. Roman V. Yampolskiy Introduction to Programming Languages Examination 1 Fall 2023 Name: ethan suttor Section: 01 User ID: 5519519 1. Please read all questions carefully prior to answering them. 2. You have 24 hours to complete this examination. 3. Books, Notes, Calculators, Friends, etc. are not allowed. 4. No Personal Digital Assistant devices, AIs or personal computers of any kind are allowed.
Problem 1: (8 points) For each of the following, write T if the statement is true and F if it is false. You must use T or F. Do not use x’s or check marks they will be counted as wrong answers . 1 A “ do while ” construct may never get executed. t 2 The “ break ” statement in a “ switch” construct causes the program to immediately exit the “ switch” construct t 3 Me_2_you is a valid C identifier t 4 C is just a shorthand name for C++ programming language f 5 If x is defined by int x = 5 ; then the value of -*&x is -5 t 6 The unary operator “-“ has a higher precedence than the multiplication operator “*“ f 7 The increment operation ++n produce the same value as n = n + 1 t 8 ? is the logical not operator in the C programming language f Problem 2: (4 points) Complete the following truth table for the logical OR operation: Input 1 Input 2 Result True True f False False f True False t False True t Problem 3: (3 points). For each of the following code segment, fill in the blank with the correct value: (a) If the following code segment has been executed: printf("%d\n", 17 / 3 + 6 / 5 % 2 + 8); The value that will be printed is: 14 with a new line after it (b) If the following code segment has been executed: int k=7, m= -4; k -= m++; Then the value of the variables k and m become: k = 11 and m = -3
Problem 4: (10 points) Examine the following code: #include<stdio.h> int main(){ int num; printf("Enter an integer: "); scanf("%d", &num); if(num <= 0) printf("This is not a valid input"); switch((num+5)%5+1){ case 7: printf("\nThe world is flat\n"); break; case 2: printf("\nFlorida is hot in the summer\n"); break; case 3: printf("\nFlorida is warm in the winter\n"); break; default: printf("I have never been to Florida\n"); } /* end switch */ system("PAUSE"); return 0; } a) If the user’s input to this program is 7 the output will be: Florida is warm in the winter b) If the user’s input is 3 the output will be: I have never been to Florida c) What is the smallest integer that will produce the output: “Florida is hot in the summer”? 1
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
Problem 5: (10 points) The following code contains 10 errors please correct all 10. #inlude <stdio.h> Main() { int iSelection = 0; float ftransAmount = 0.0; float fBalance = 100.25; printf("\n\tATM\n"); printf("\n1\tDeposit Funds\n"); printf("2\tWithdraw Funds\n"); printf("\nEnter your selection: "); scanf("%d", &iSelection); if (iSelection = 1) ; printf("\nEnter fund amount to deposit: "); scanf("%f", ftransAmount); printf("\nYour new balance is: $%.2f\n", fBalance + fTransAmount); } end if if (iSelection = 2); { printf("\nEnter fund amount to withdraw: "); scanf("%f", &fTransAmount); if (fTransAmount > fBalance); printf("\nInsufficient funds\n"); } else { printf("\nYour new balance is $%.2f\n", fBalance - fTransAmount); } end if system("pause"); } //end main function
Problem 6: (10 points – 2 points each) Students in the C/C++ computer lab are busy writing: a) Binary code b) Digital code c) Source code d) Executable files C Which operator has the highest precedence? a) + (add) b) % (mod) c) * (multiply) d) ! (not) C Which of the following is not a reserved C keyword? a) do b) While c) if d) double B Which of the following variables is not legal in C? a) MyName b) YourName c) 1day d) Seven_ C Which of the following is not a C logical operation? a) not b) maybe c) or d) and B
Problem 7: (10 points) a) Write a complete (with all necessary declarations: prototype, definition, call) function called myPower() that takes two integers m and n and returns m raised to the power n. In doing this you may not use any mathematical functions in C such as pow(x,y). You may only use C constructs such as loop ... etc, and C operands. #include <stdio.h> int myPower(int m, int n); int main() { int base, exponent; printf("Enter the base: "); scanf("%d", &base); printf("Enter the exponent: "); scanf("%d", &exponent); int result = myPower(base, exponent); printf("%d^%d = %d\n", base, exponent, result); return 0; } int myPower(int m, int n) { int result = 1; if (n < 0) { printf("no negative exponents \n"); return -1; } for (int i = 0; i < n; i++) { result *= m; } return result; }
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
b) Explain if the changes done by the actions of this function are permanent or not and state why. It is not permanent because the original parameters, are not affected by the function Problem 8: (10 points) Write an interactive program that calculates the volume of a cylinder using v= πr²h where π = 3.14, v = volume, r is the radius of the base, and h is the height of the cylinder. Your program must accommodate any range of values. You must prompt the user for each of these measures and ensure that they are valid values, i.e. no cylinder will have a negative or a zero dimension (don’t worry about non-numeric input from the user). The program must print out the correct volume. Example: if I input 2 for radius and 3 for height the program should inform me that the volume of cylinder is 37.68. #include <stdio.h> int main() { const double PI = 3.14; double radius, height, volume; do { printf("Enter the radius of the cylinder: "); scanf("%lf", &radius); if (radius <= 0) { printf("Invalid input. Please enter a valid number.\n"); } } while (radius <= 0); do { printf("Enter the height of the cylinder: "); scanf("%lf", &height); if (height <= 0) { printf("Invalid input. Please enter a valid number.\n"); } } while (height <= 0); volume = PI * radius * radius * height; printf("The volume of the cylinder is %.2lf\n", volume); return 0; }
Extra Space