Unformatted text preview:

CS 120 Java Practice1. (5) The components of a method header includea. name, return value, prototypeb. argument list, parameter list, namec. method body, name, argument listd. return type, name, parameter list2. (5) All variables must havea. a name and a typeb. an expression and a specifierc. syntax and logicd. memory location and status flag3. (5) A String variablea. stores a String objectb. provides a reference to a String objectc. stores a String literald. can hold up to 80 characters4. (5) An algorithma. is similar to a logarithmb. is a step-by-step solution to a problemc. concentrates on the syntactic solution to a problemd. is not needed when using the Java language5. (5) A static variablea. is allocated once for each object of the classb. is an unchanging quantityc. is shared among all the objects of the classd. only exists if objects are instantiated6. (5) The general form of a for loop contains three components. Whichof the following lists the correct components in the correct order?a. boolean expression; increment; decrementb. initialization; boolean expression; update statementc. initialization; update statement; boolean expressiond. start value; end value; increment value7. (5) Which of the following is not a primitive type?a. intb. doublec. byted. string8. (5) In a switch statement, the keyword defaulta. is accessed if none of the other case values match the controlexpressionb. causes the program to exit the switch statement completelyc. causes the program to re-evaluate the control expressiond. causes the program to terminate9. (5) Which method allows you to retrieve individual characters in aString?a. getChar()b. toString()c. copyValueOf()d. charAt()10. (5) In the statement System.out.println( myString); which of theidentifiers represents a class.a. Systemb. outc. printlnd. myString11. (5) The constant 3.14159 is a(n)a. integerb. character (π)c. doubled. float12. (5) When a method is called, the values of the arguments area. copied into the corresponding parameter variablesb. returned to the callerc. shared between the caller and the called methodd. assigned to the parameters that have the same name as thearguments13. (5) Which loop will print the even numbers from 0 to 10;a. int i = 0;while (i < 10) {System.out.println(i);i += 2; }b. int i = 0;while (i <= 10)System.out.println(i);i += 2;c. int i = 0;while (i <= 10) {System.out.println(i);i ++; }d. int i = 0;while (i <= 10) {System.out.println(i);i += 2; }14. (5) What is the effect of the following code fragment?int choice = 2;if (choice ==2)System.out.println( "Happy Birthday");elseSystem.out.println( "Have a good day");System.out.println( "Have a good day");a. Happy Birthday is printed.b. Happy Birthday is printed followed by Have a good day..c. Have a good day is printed twice.d. Nothing is output because there is a syntax error.15. (5) A method can give back to the calling methoda. many values using the return statementb. one value using the method headerc. one value using a specially designated parameterd. one value using the return statement16. (8) Declare variables to serve the following purposes:a. The price per pound for apples.b. The number of students in a class.c. The title of a book.d. A constant representing the charge on an electron (1.6× 10−19)17. (10) What is the output of the following code?int i = 4;while (i >= 1) {int j = i;while (j >= 1) {System.out.print( j + " ");j--;}System.out.println( " : " + i);i--;}18. (20) Evaluate the expressions below. Show your steps.a. 45 / 8 * 4 + 2b. 21 % 6 * 1.2c. 3 * (int)7.8d. 3 + 4 > 7 || 6.3 / 7 < 1e. 3 * 4 > 10 && ! false19. (10) Write Java expressions for the following:a. sum =n2(2 a + (n − 1) d)b. V =43π r320. (10) Show the values of p, n, d and q after each segment of the followingcode has executed.int p=68, n=0, d=0, q=0;q = p / 25;p = p % 25;p = n = d = q =d = p / 10;p = p % 10;p = n = d = q =n = p / 5;p = p % 5;p = n = d = q =21. (10) Write an algorithm, a detailed set of instructions in English, to gothrough a stack of four tests and pick out the one with the highestscore.22. (10) Write a program to calculate the area (π r2) and circumference(2 π r) of a circle. Use variables of appropriate type to store theradius of the circle and the results for the area and circumference.Initialize the radius to 3.2, then calculate the results. Finally, printout all three values with enough text to make it clear what eachvalue represents.23. (10) Write a method that calculates (and returns) the volume of a rightcircular cylinder from the radius and height. The volume is πr2hwhere r is the radius of the circular cross-section and h is the heightof the cylinder.24. (10) Given the following declarationsString superhero = "Batman";what are the values of the following expressions?a. superhero.length()b. superhero.charAt(4)c. "C" + superhero.substring(1, 3) + "wo"+ superhero.substring( 3, 6);25. (10) Write a method called inRange that takes an integer parameter, x,and returns either true or false depending whether x is in the rangeminV alue ≤ x ≤ maxV alue. You should assume that minValueand maxValue are declared in the class containing inRange and thatthey already have values.26. (10) Write a loop that reads values of distance and calculates the time totravel that distance in a car traveling at 55 miles per hour. Displaythe distance and time (in hours) for each entry. The loop shouldexit when a distance of 0 is entered. You can assume a Scannernamed kbd has already been


View Full Document

BOISE STATE CS 120 - practice

Documents in this Course
Load more
Download practice
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view practice and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view practice 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?