java multiple choice
How do you declare a variable that will store an instance (more accurately the location in memory of the instance) of the reference type (i.e. compound type) Character? a. Character val b. val = new Character c. public class Character val d. (Character) val
a
What is the Java code needed to access the 5th character of a String that has its address stored in the variable s? a. s.charAt(4) b. s[4] c. s.charAt(5) d. s[5]
a
What is the difference between typecasting a primitive type and typecasting a non-primitive type? a. Typecasting a primitive changes the binary value of the primitive. Typecasting a non-primitive does not change the value, but it changes how we can use the value. b. Primitive values can be typecast to any other primitive (except boolean). Non-primitive values can only be widened, they cannot be narrowed. c. There is absolutely no difference other than the primitive value is the value itself and the non-primitive is an address where the actual value is stored. Otherwise the typecast behaves exactly the same. d. Typecasting a primitive value (that is not boolean) can change the value to anything but boolean. It is illegal to typecast a non-primitive value.
a
What is the type of each of the following Java expressions? a. 100 * 0.5 b. (int)4.0 / (int)3.0 c. 4 + (100 - 40L) d. 'B' + 1 e. (byte)3 - (byte)1 OPTIONS: -double -int -long -float -byte -short -char
a. double b. int c. long d. int e. int
Suppose you need to create an instance method that can be used in any part of a Java program. The method is called average, and it takes two input values each of type int and returns a value of type double. Match the blanks below with the appropriate code: 1:???? 2:???? 3:???? (4:????) { a. Blank 1 b. Blank 2 c. Blank 3 d. Blank 4 OPTIONS: double value1, double value2 class public int double int value1, value2 average private method double value1, value2 int value1, int value2 static
a. public b. double c. average d. int value1, int value2
How do you call (i.e. execute) the getHeight method on an object whose location is stored in the variable named j? a. j->getHeight() b. j = getHeight() c. j.getHeight() d. getHeight(j)
c
Suppose we have a "two-dimensional array"int[][] array How many elements are in the second "row" of array? a. array[].length b. array.length c. array[1].length d. array[][].length
c
What changes can you make to an array after you create it? a. You can change both the contents and the length of an array at any time. b. You cannot change the contents of an array, but you can change its length. c. You can change the contents of the array, but you cannot change its length. d. You cannot change the contents or the length of the array after it is created.
c
What does super do? a. super is used to access the true type of the super class. b. super. (with a dot) is used to access a field of the super class and super() (with parentheses) is used to access a method of the super class. c. super. (with a dot) access a method or field of the super class, and super() (with paretheses) calls a constructor of the super class. d. super is a special variable in instance methods that stores the reference to the super class instance.
c
What is a type in a programming language? a. How the computer stores a piece of data. b. The font that you use to write the program. c. What the programmer decides that a piece of data should represent. d. It classifies what the programmer does.
c
What is the value stored in i at the indicated spot? int i = 0; while (i <= 10) { System.out.println("Hello"); i = i + 1; } <---- what is the value of i here? a. Variable i does not exist at this point in the code. b. 10 c. 11 d. 12
c
What is the value stored in i at the indicated spot? for (int i = 10; i >= 0; i = i - 1) { System.out.println("Hello"); } System.out.println("Loop done"); <---- what is the value of i here? a. -1 b. 0 c. Variable i does not exist at this point in the code. d. 10
c
What loop will correctly run through all the elements of an array? a. for (int i = 1; i < a.length; i = i + 1) b. for (int i = 0; i <= a.length; i = i + 1) c. for (int i = 0; i < a.length; i = i + 1) d. for (int i = 1; i <= a.length; i = i + 1)
c
Beginning programmers often have trouble with if statements. It is not the logic of "if" that trips up students, but rather it is easy to make misleading statements by using incorrect syntax or not having a good coding style. The following statement was written by a beginning programer and contains a couple of unfortunate choices and typos. Read the following statement carefully and indicate what is stored in x at the end of the execution. int x = 10; boolean test = false; if (test = false) x = x + 5; x = x * 2; x = x - 1; a. 29 b. 14 c. 9 d. 19
d
What is returned by the Java new operator? a. A new value that you can use in your program. b. A new empty class prepared so that you can add any fields and methods you want to it. c. The object (i.e. the class instance) that the new operator just created. d. The location in memory of the instance the new operator just created.
d
What is the first statement in the body of a constructor? a. It is a call to the new operator to create an instance of the class. b. It is always the statement: super(); c. It can be anything at all. A constructor is just a method. d. It is always a call to another constructor of either the same class or the parent class (even if we do not explicitly type in the constructor call).
d
What is the form of a constructor? a. It is a special method with the name constructor. b. It has the term constructor placed in front of the method header. c. It is a special method with the name new. d. It is a method with the same name as the class and no return type.
d
What is the word this in Java? a. It is used inside the body of an instance method (i.e. a non-static method), and it is a reference to the body of the method. b. It is used inside a class, and it is the location in memory of the class. c. It is the opposite of that. d. It is used in the body of an instance method (i.e. a non-static method), and it is the location in memory of the object (i.e. class instance) that the method is acting on.
d
When we need to create a String, piece by piece, inside a loop, what should we use? a. the + operator b. the << operator c. the String class d. the StringBuilder class
d
Which of the following is not correct Java? a. int[][] a = null; b. int[][] a = new int[5][10]; c. int[][] a = new int[10][]; d. int[][] a = new int[][6];
d
Indicate which of the statements below are true and which are false. a. All Java programming consists of writing classes or other reference/compound types. b. Every class you write must extend exactly one other class of Java. c. You must write at least one method in every class that you create. d. Every public class must go in its own separate file with the same name as the class.
a. true b. true c. false d. true
What does it mean to override a method in Java? a. You change the behavior of a method in a class so that if you typecast an instance to the class, you will get the new method behavior. b. You change the behavior of a method so that all instances of a true type (no matter what the current type is) will use the new version of the method. c. You create multiple versions of a method, and you can choose which version to call by changing the inputs to the method. d. You block a class from inheriting a method from its super class.
b
What does the word static do when it is placed in front of a field declaration? a. It indicates that the value of the field is primitive and not a reference type. b. It indicates that the field is stored in the class and not in an instance of the class. As a result, all instances of the class will share a single copy of the variable. c. It indicates that the value of the field will not change once it has been set. d. It indicates that there will only one field of this name.
b
What expression evaluates to the fourth element of array a? a. a(4) b. a[3] c. a[4] d. a(5)
b
What is the correct way to convert the following value of type double to type int? a. (13.516)int b. (int)13.516 c. double->int(13.516) d. (int 13.516)
b
What is the correct way to: 1. declare (i.e. create) a variable called percentage that will store values of type double values, and 2. store the value 65.0 into the variable named percentage? a. (double) percentagepercentage <- 65.0 b. double percentagepercentage = 65.0 c. double percentage65.0 -> percentage d. (double) percentagepercentage == 65.0
b
Which of the following expressions has the value true and does not change the value stored in the variable z? a. (3 > 6) || ((z = 10) > 100) b. (3 < 6) || ((z = 10) < 100) c. (3 < 6) && ((z = 10) < 100) d. (3 > 6) && ((z = 10) < 100)
b
Which of the following expressions has the value true and does not change the value stored in the variable z? a. (3 > 6) || ((z = 10) > 100) b. (3 < 6) || ((z = 10) < 100) c. (3 > 6) && ((z = 10) < 100) d. (3 < 6) && ((z = 10) < 100)
b
In the statement: for (int i = 0; i < 3; i = i + 1) x = x * 10; List the order each statement and expression is evaluated. Executed/evaluated first: Executed/evaluated second: Executed/evaluated third: Executed/evaluated fourth: Executed/evaluated fifth: Executed/evaluated sixth: Executed/evaluated seventh: Executed/evaluated eighth:
int i = 0 i < 3 x = x * 10 i = i + 1 i < 3 x = x * 10 i = i + 1 i < 3