270 Review Questions
1. Match the language to the paradigm: (one paradigm has 2 languages in this list)
Functional
Languages |
Fill in match: |
Paradigm letter |
Paradigms |
Java |
|
DL |
Declarative Logic Programming |
Racket |
|
DF |
Declarative Functional Programming |
Prolog |
|
IP |
Imperative Procedural |
C |
|
IOO |
Imperative Object Oriented |
C++ |
|
|
|
2. In which paradigm does the sequence of commands matter: functional or declarative?
3. In which paradigm would I sum = 0 and start a loop and accumulate within the loop?
a. How would I do that same accumulation in the opposite paradigm?
4. I said declarative languages are about what, and Imperative languages are about how. Give an example to show why that is true.
5. Which language(s) give you fine control over memory? What are the advantages and disadvantages to that?
6. Which language might be best for processing language applications with large vocabularies?
7. IF you were designing a C++ program, what would be the first thing you started designing? If you were designing a racket program, what would be the first thing you started designing? (Think of your own project design doc.)
8. C does not have classes the way C++ does. What tool (or construct) does C have that is similar to a class with instance variables and no methods?
9. How do you pass by reference in C? Consider primitive variables, arrays and structure variables and pointers. How do you pass by reference in C++? Consider primitive variables, arrays and structure variables and pointers, plus objects and reference variables.
Why might you want to pass by reference?
10. Write a Racket program that defines a variable taxrate and then defines a function to take in your gross pay and return your netpay. Gross - Gross * taxrate is netpay when your gross is over 20000. If it is lower, your taxes will be a flat 600. Call the function with 20,000 and 800 and print the difference in net pay.
11. Write the same tax rate program in C, asking the user for the gross and displaying the net pay on the screen.
12. Write the same program in C++, but create a TaxCalculator class that has instance variables of taxrate and gross, and also has a constructor that takes in both and also has a calculateTax method. Write a static main method that will create two TaxCalculator and ask the user to enter the gross pay for both and then the program should tell both net pays and the difference between them. If the pay is negative, the TaxCalculator should throw an exception rather than allow the negative into its object.
13. Write a knowledgebase in prolog of 10 facts about people's gross pay:
a. Mary(10000)
b. Amy(3000)
c. Willie(1000)
d. Etc
And then write a rule about earnsMore so that you can ask whether one person earns more than the other.
14. Structures and arrays: Write a C program that has a puzzle variable that is an array of a structure of one int named display and the int named answer. (One puzzle array of a structure. The structure has int display and int answer). Create the puzzle array to be 3 long. Fill the answer with 1,5,2 and set the display to 0,5,0. Create a method that takes in the array and sets the display to the answer for every answer greater than 1. It should also print the size of the array inside the method. Run that method and then print the display values to the screen and then print the answer to the screen. Did the array pass by reference or by value? Did you need to define the pointer? How did you know the size of the array when you were in ?
15. Strings in c: Write a c program that creates 2 character variables, one that is a pointer to a character and the other that is a character array. Ask the user to enter a word less than 10 characters to store in each of those variables and read the words in using scanf. Append the character array's word to the character pointer's word. Print the character pointer's word. Did you need to use the & on scanf? Why? Why did you need to allocate memory for the pointer to a character and not for the character array? Why couldn't you use + to concatenate the strings?
16. Strings in c++ : Copy the program from 17 into a c++ program. Change to iostream and use cout and cin to take in the name values. Add one more variable, a std::string, and use cin to take in that one also (getline(cin, string, ending char such as '\n') Append all three words onto the std::string variable. Why did you have to convert the cstrings to std::strings before appending? Why didn't cin work wth same way with the std::string as it did with the c style strings?
17. Allocating memory: Create a c++ program that has 2 array variables with no size. Allocate memory for one using malloc and for the other using new. Fill the arrays with zeroes. Print them. And then deallocate memory for both? Could you use free to release memory for both array types?
18. Create a c++ function that takes in one pointer, one array, one integer element of an array, one reference variable, and one integer. It should change every variable and return an integer. Call it from another method and save the return variable. Then, print every variable that was sent and the return variable. Could you have guessed the result?
19. Add numbers from 1 to 10 in c and in racket.
20. There is a list filled with 2 types of computers, laptops and desktops. Desktops all cost 100 and laptops all cost 200. In racket, C, Prolog and C++, code a program that sums the total of all the computers in the list. Use inheritance and polymorphism in c++.