Java Midterm

Ace your homework & exams now with Quizwiz!

5. Which of the following commands would be used to execute a Java class named First?

java First

11. Which package of Java classes is automatically available to every Java program?

java.lang

15. Every Java application must contain a main method. Which of the following is the correct definition of that main method?

public static void main(String var[])

18. Given this code segment: Rectangle rec1 = new Rectangle(10,20,30,40); Rectangle rec2 = rec1; What is the result when you try to compile and execute the code.

rec1 and rec2 contain references to the same object

37. A String variable name is

reference

28. The Java expresion Math.pow(side,2) is the same as

side*side

47. What will be the values of x and y as a result of the following code? int x = 12, y = 5; x += y--;

x=17, y=4

36. Java requires you to cast whenever:

you convert a String to an int.

41. If String movie = new String("WestSide Story"); , then the value of movie.indexOf('s') is

2

46. Assuming a variable w has been assigned the value 15, then the statement x = (w == 15 ? 2 : 0); assigns _____________.

2 to x

43. What would be the value of x after the following statements were executed? int x = 10; switch (x) { case 10: x += 15 case 12: x -= 5; break; default: x *= 3; }

20

26. What would be the value of bonus after the following statements are executed? int bonus, sales = 1250; if (sales > 1000) bonus = 100; if (sales > 750) bonus = 50; if (sales > 500) bonus = 25; else bonus = 0;

25

44. What will be printed when the following code is executed? double x = 45678.259; DecimalFormat formatter = new DecimalFormat("#,###,##0.00"); System.out.println(formatter.format(x));

45,678.26

42. What will be the value of pay after the following statements are executed? int hours = 45; double pay, payRate = 10.00; pay = hours <= 40 ? hours * payRate : 40 * payRate + (hours - 40) * payRate * 1.5;

475.00

Given String city = "Saratoga"; what is the value of city.length()?

8

4. The Java Programming Language requires that every Java application program have a method named _________.

Main

10. This computer was about 55 ft. long and 8 ft. high and was built in the 1940s

Mark I

12. Which of the following is a valid reference to the constant PI defined in the Math class?

Math.PI

23. In general, the technique used to access an instance variable from outside the class is to:

Mental telepathy

25. Verbs in the problem definition statement are very useful for identifying:

Methods

20. Which of the following defines an object variable of type Rectangle?

Rectangle myRec;

6. Which of the following is NOT one of Java's primitive data types?

String

1. The Java programming language was developed by ___________________________

Sun Microsystems

49. What is wrong with the following code? public class Class1 { public static double method1(double x) { return x * 33.4; } public static int method1(double x) { return (int)(x * 33.4); } }

The signatures are the same.

22. What will be the value of x after the following code is executed? int x = 75; int y = 60; if (x > y) x -= y;

15

45. Which of the following will format .12.78 to display as 12.8%?

##0.0%

9. By default, all numeric fields for an object are set to ____________ when the object is first instantiated.

0

8. What is the result of evaluating the expression 45 % 2?

1

48. What will be the value of x after the following code is executed? int x = 10; while (x < 100) { x += 10; }

100

7. A byte can store a number as high as _____________.

127

35. Given the folowing code segment: String name = "MacBeth"; char ch = name.charAt(3); When this code executes ch will be set to what?

B

51. What is the output of the following code segment? s = 20; if(s > 30) System.out.print("AAA"); System.out.println("BBB");

BBB

13. What does the Java programming language allow you to put prior to an import statement in your Java program?

Blank lines or comments

19. The process of hiding the object data and providing methods for data access is called

Encapsulation

2. The technique of packaging an object's attributes and methods into a cohesive unit is called

Encapsulation

16. What is the basic function of the Java interpreter?

Execute bytecode instructions.

24. The main purpose of a constructor is:

Initialize instance variables

34. Which of the following statements will prompt the user for an input string?

JOptionPane.showInputDialog("What is your age?");

3. ____________________ invented a weaving loom controlled by punched cards.

Jacquard

50. If the following were declared as the constructors for class Rect, which would be considered the default constructor? (1) public Rect(int len, int width){...} (2) public Rect(double len, double width){...} (3) public Rect(int){...}

a. (1) b. (2) c. (3) d. (1) and (3) e. None of them, this class will not have a default constructor. E

40. The substring() method requires ______ arguments

a. 0 b. 1 c. 2 d.3 e. b or c E

27. Given int interest; Which of the following can be assigned to interest?

a. 10 b. 5.25 c. 6.0 d. 0 e. a, d, and f f. -3 E

29. Which of the the following are Java naming conventions?

a. Classes start with an uppercase letter. b. Methods start with an uppercase letter. c. Object start with a lowercase letter. d. Objects start with an uppercase letter. e. a, c, and f f. Methods start with a lowercase letter. g. Classes start with a lowercase letter. E

17. Which of the following are VALID variable names?

a. public b. person1 c. 3Cats d. myRec e. b, d, and f f. MY_3_CATS g. the-rec E

30. Given int quantity = 3; double value = 7.52; String number = "one"; Which of the following are valid assignments?

a. quantity = value; b. quantity = (int)value; c. value = quantity; d. quantity = (double)value; e. Both b and c f. quantity = number; E

31. The method used to compute the length of the string abc is

abc.length()

14. What will be the values of ans, x, and y after the following statements are executed? int ans = 35, x = 50, y =50; if ( x >= y){ ans = x + 10; x -=y;} else ans = y + 10; y += x;

ans = 60, x = 0, y = 50

33. Given String input = "4 2"; int answer; Which statement will set the integer value 42 into the variable answer?

d. answer = Integer.parseInt(input);

39. The method that determines whether two objects are equivalent without regrd to case is

equalsIgnoreCase()

38. If you declare two String objects as String word1 = new String("happy"); String word2 = new String("happy"); Then the value of word1 == word2 is

false

21. If you do not explicitly set a numeric instance field, the instance field

is initialized to 0.


Related study sets

troubleshooting methodology/resolving core HW Prob

View Set

Statistics - Ch5 z Scores and standardized distributions

View Set