RU beehive logo ITEC dept promo banner
ITEC 120
2007fall
ibarland,
jdymacek

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs

exam1-study-questions
exam1 study sheet
2007.Fall

Exam1 is scheduled for class on Sep.20 (Thu). The exam will be in-class, on-paper, closed-computer. It will consist of questions very much like these questions, the homeworks, and labs. Some of the questions may be directly from these sources. The single best way to study for the exam is to sit down and re-work homework and lab questions from scratch.

You should know the syntax for declaring classes, writing methods (including constructors), calling methods (including constructors), creating instances (with new), declaring local variables, initializing local variables, and declaring+initializing named constants. You will need to know the following types: int, double, boolean, String; the keywords class, return, if-else, if-else-if, this, and the javadoc keywords @return and @param. (And, maybe others I forgot to list here.) You will need to know about Java's arithmetic (precedence of *,/ over +,-, integer division (quotient) vs. floating-point division, remainder (%), and the potential of round-off error). You will need to know the logic operators !, &&, ||.

When giving a java expression or definition, you should be able to type it into BlueJ's Code Panel without any error; this means knowing where you need parentheses, where you need squirly-braces, and where you need semicolons (though I'll give some slack on these). I particularly recommend the problems which require actual code or definitions.

  1. Review the lecture notes, and write for yourself any of the sample programs/exercises therein.
  2. Review the labs, and write (from scratch) any of those programs again.
  3. Review the homeworks, and write (from scratch) any of those programs again.
  4. What is the mantra for calling a function?
  5. What is the mantra for a function's signature?
  6. (2pts) Give the signature of a function balanceOf which takes in a String (a student's name), and returns an int (their RU Express account balance, in cents).
  7. (3pts) Give the signature of a function which takes in a String and an int, and returns a double. (Use whatever names you like.)
  8. (2pts) The function upcase takes a String, and returns that same text but in uppercase. Write the signature for upcase.
  9. (2pts) Suppose Widget were a Java type. What would be the signature for a function widgetRating which took in a Widget and returned an int?
  10. (2pts) In Java, what two functions are each named +? (give the English word(s) for them.)
  11. (2pts) In Java, what two functions are each named /? (give the English word(s) for them.) 1
  12. (2pts) Trick question: In Java, what does the function2 named “+” do?
  13. (2pts) Trick question: In Java, what does the function named “/” do?
  14. (1pt) Consider the Java expression 33 + " blind mice". Which one of the following is true?
    1. This expression is illegal in Java, and won't compile (it will generate a syntax error), since you can't add an int to an String.
    2. The expression will compile without syntax errors, but will generate an error (“exception”) when the program is run and the expression is evaluated, since you can't add an int to an String.
    3. Java first calls a function to convert 33 into a string, and concatentates the result with " blind mice".
    4. Java first converts " blind mice" into a number, and adds the result to 33.
  15. (1pt) True or False: Java has two ways to include comments: (A) From // to the end of a line, and (B) from /* to the next */ (which can span multiple lines).
    (You can write a small program to try this out, if you're not sure.)
  16. (1pt) What is wrong with the following excerpt from a javadoc comment?
    /**   ...
     * @param The diameter of the pizza, in inches.
     *    ...
     */
    
  17. (1pt) Is the following a valid series of Java statements?
      double ROOM_LENGTH = 10;      // Height in feet.
      double ROOM_WIDTH  = 2 * ROOM_LENGTH;  // In feet.
      double ROOM_HEIGHT = ROOM_LENGTH * Math.sqrt(5) / 2.0;  // In feet.
    
  18. (1pts) What is the result of Java evaluating 25/10?
  19. (1pts) What is the result of Java evaluating 25/10*10?
  20. (2pts) Insert parentheses into the preceding expression, so that the result is 0.
  21. (1pts) Why is it not obvious that Java evaluates 25.0/10.0*10.0 to be 25.0?
  22. (2pts) Rewrite the following expression for clarity (without changing the result): 2*3+16/2/2*3-1 (You might want to type in the before- and after- versions into Code Pad, to make sure they agree.)
  23. (6pts) Naming conventions. Match each of the following with its naming convention (below):
    1. function names
    2. local variable names
    3. parameter names
    4. primitive types (that is, types built-in to Java, not a class)
    5. class names (that is, non-primitive types :-)
    6. named constants
    with one of:
    1. all capital letters (and underscores) (e.g. MINS_PER_GIG)
    2. capitalize only the first letter of each (sub)word, including the first letter (e.g. PizzaServer)
    3. capitalize only the first letter of each (sub)word, not including the first letter (e.g. numPetsInStock and diam)
  24. (1pt) Show an integer-arithmetic expression where Java gives an incorrect answer.
  25. (1pt) Show an floating-point (double) arithmetic expression where Java gives an incorrect answer.
  26. (2pts) [Challenge] Which one of the following primitive types is archaic, and should be rarely used?
    1. int
    2. long
    3. double
    4. float
  27. (2pts) [Challenge:] What is a circumstance in which you might use the numeric type short, even though it can't represent numbers bigger than 32,000 (approx)?
  28. (1pt each) For each of the following, name the Java type best suited to represent it:
    1. The price of a gallon of gas, in dollars.
    2. The price of bananas per pound, in cents.
    3. A person's height.
    4. A student's name.
    5. Whether or not that student is on the President's honor roll.
    6. The number of classes a student is taking this semester.
    7. A person's zip-code.
    8. A number-of-stars entered by a user when rating a DVD (say, on amazon or Netflix or imdb.com or rottentomatoes.com).
    9. Whether or not a student has finished their Gen Ed requirement.
  29. (2pts each) For each datum in the previous question, declare a Java variable (or parameter) to represent it and initialize it to a plausible value. Use a descriptive name, and (as necessary) include a short comment (to describe units, or further info) For example, to represent “the current speed of a horse-and-buggy”, one answer would be:
      double buggySpeed = 0.0;   // In leagues-per-fortnight.
    
  30. (1pt) What is a(nother) real-world value which is best modeled in Java with an int?
  31. (1pt) What is a(nother) real-world value which is best modeled in Java with a double?
  32. (1pt) What is a(nother) real-world value which is best modeled in Java with a String?
  33. (2pts) Assuming Widget were a java type, how would you declare (and initialize) a variable of type Widget? (Since we're not sure what an initial value for a Widget might be, just write “...” instead of an initial value. Don't forget any semicolons!)
  34. Suppose we are starting to think about writing a function which takes two integers, and calculates their average.
    1. (1pt) What is a good name for this function?
    2. (3pts) What are a couple of test cases?
    3. (2pts) Write the signature3 for this function.
    4. (2pts) Write the entire function.
  35. Write a function myAbs, which takes in a number and returns its absolute value -- that is, it returns a number which is positive, regardless of the sign of the input.
  36. (3pts) Consider the legal Java expression 22*TAX_RATE-4*3/2+1
    1. Rewrite the expression for clarity by adding spaces and parentheses only (without changing its value, or doing any arithmetic yourself).

       

    2. What does the expression evaluate to, if we have declared the named-constant double TAX_RATE = 0.50; /* That's a hefty tax rate! */

       

    3. What does the expression evaluate to, if we had instead declared double TAX_RATE = 50/100; /* Is this tax rate really so bad? */ (Hint: Java doesn't give the same answer for (b) and (c)!)

       

  37. (2pt each) You have landed a job operating the scoreboard for a football team. For each of the following, declare a variable of the appropriate type. It should be appropriately named, and initialized to some reasonable value.
    1. The name of the team.

       

    2. The team's score during a game.

       

    3. The team's average yards-gained-per-play, this season.

       

  38. Sometimes coaches need help in deciding what to shout at their team, during a game. Of course, what they should shout depends on many points ahead (or behind) their team is.
    1. (2pts) Write the signature (and only the signature) of a function which takes in one int (representing how much the team is ahead by -- that is, the team's current score minus their opponent's score), and returns a String (something the coach might shout out, based on that score). Use appropriate names.

       

       

    2. (3pts) Write (at least) three test cases for the function in the previous problem. Be sure to indicate both what the input is, and an plausible output.

       

       

       

    3. Write the body for the above function.
    4. (4pts) The coaching-comments program for which you wrote the signature above has been completed by other programmers; it works well, and your boss is happy with it. But it's admittedly a bit annoying, to force whoever is calling that function to have to compute the difference between the scores. Write a second function which takes two inputs--the score of the coach's team and the score of the opposing team--and returns a message for the coach to shout. (Write the entire function, although one test case will suffice.)
      Hint: re-use existing code, rather than repeat any code!
  39. How is Java's “=” pronounced?
    What is java for “equals” (with respect to numbers and booleans, at least)?
  40. What type of statement is double myNumber; (according to our syntax)?
    What type of statement is myNumber = 17;?
  41. To give a name to a sub-computation, declare and initalize                 .
  42. Unlike a local variable, a named constant does not depend on                 .
  43. Write a function...
    1. feetAndInchesToMeters: If somebody is 6′4½″ tall, they are 1.9431m tall. How many inputs to this function? (Remember, there are 2.54cm/in.)
    2. lbsToKg: If somebody weights 220lbs, they weigh 100kg.
    3. bodyMassIndex: Somebody who weighs 220lbs and is 6′4½″ tall has a body mass index of 26.5 (approx). Write a function which, given a person's weight (in pounds) and height (how many feet and how many inches), returns their body mass index. The general formula for BMI is simple: The person's weight in kg, divided by their height in meters squared.4
    4. bodyMassIndexType: Given the same info as the previous problem, return the person's weight category.
      BMI intervalcategory
      [0,18) underweight
      [18,25) normal
      [25,30) overweight
      [30,∞) obese
      Note that the BMI scale has its limitations.
  44. What is the syntax of an assignment statement? (Assigning to a local variable, or to a field)
  45. Recall that in the EBay Fee problem from lab, calculating the final-value-fee involved three different tiers: the interval [$0,$25), [$25,$1000), and [$1000,∞).
    What are good test cases, which test all the boundary conditions, as well as pertinent non-boundary conditions?

1We are not talking about comments. Comments certainly aren't function, and besides they need more than a single slash.      

2Well technically, + is an “operator”, since we don't use parentheses to call the function      

3The first problem describes what a function's signature is.      

4 This formula takes a person's weight, adjusting for how tall they are. The squaring stems from the fact that if person A is (say) 10% taller than person B, then A is also allowed to be 10% wider, but not 10% thicker. So you square the person's height, rather than cube it.      

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs


©2007, Ian Barland, Radford University
Last modified 2007.Sep.16 (Sun)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme