COP2250
For(int I = 0; i < 6; i++) { For(int j = 1; j <= 5; j++) { System.out.print(I + j + " "); } System.out.println(); } What is printed by the third iteration of the outer loop?
3 4 5 6 7
int [ ][ ] x = {1, 2}, {3, 4}, {5, 6}; What are x.length and x[0].length?
3 and 2
Int n; For(n=8; n < 30; n += 5) { // } System.out.print(n); What is printed when the loop above has finished?
33
What is the result of this statement? System.out.println(1 + 2 + "b");
3b
int x = 3; int y = 5; int z = x++ > --y ? x : y; System.out.print(z); What (if anything) is printed?
4
int z = 2. sum = 0; while (z < 9) { z++; sum = sum + z; } System.out.print(sum); What is the output of the code above?
42
String a = "4"; A+="5"; A+=6; System.out.print(a);
456
StringBuilder sb = new StringBuilder(); sb.append("Hey Java"); System.out.println(sb.capacity() - sb.length());
8
int = 2; int y =5; int z = x-- + (++y); What is the value of z after the statements above?
8
long loan = 1234; int number = loan; The code above does not compile. How could the code be corrected? Select ALL that apply.
Add a cast to long Add a cast to int
This contains the JVM and Java Class Library.
JRE
A device needs a ____ to run a Java program
JVM
Scanner input = new Scanner(System.in); System.out.print("Enter your name "); String name = input.next(); System.out.println(name); If the user enters James gosling when the code above runs, what is printed?
James
public static void main(String[] args) { ArrayList<String> langs = new ArrayList<String>(); lands.add("Java"); lands.add("C++"); lands.add("Python"); lands.add("2,null"); lands.add("2,null"); lands.add("4, "PHP"); for(int i = 0, i < langs.size(); i ++ ) { System.out.print(langs.get(i) + " "); } } What is the output?
Java C++ null null PHP Python
To run the compiled file Prog.class from the command line, enter ______.
Java Prog
Identify the method that returns the number of characters in a string.
Length()
What term describes a variable that is declared inside a method?
Local variable
What does the public visibility modifier mean?
Means accessible from any other classes.
6. ArrayList<Integer> counts = new ArrayList<>(); 7. counts.add(10); 8. counts.add(new Integer(8)); Identify the true statement regarding the code above.
No errors, code compiles okay.
3. public class Person { 4. 5. public Person() { 6. 7. } What type of constructor is illustrated by lines 5 through 7?
No-arg
For (int n = 0; n <= 12; n++) { System.out.print(n + ""); } System.out.println(n); What is the output of line 7?
Nothing. Compilation fails
Which expression returns false if num equals 5?
Num <= 5 ^ num % 2 == 1
Java's thousands of classes are organized into ___?___
Packages
A while loop is a __?__ loop.
Pretest
Data fields are encapsulated with which modifier?
Private
int num = 3; if (num >= 0) if (num == 0) System.out.print("first string"); else System.out.print("second string "); System.out.print("third string "); What is the output by the code above?
Second string third string
The methods of the Math class are __?__ methods?
Static
To be capable of being called in the main method of a Java program, a method defined in the same class must have the __?__ modifier?
Static
To obtain the current second, use ______.
System.currentTimeMillis() / 1000 % 60
1. This tests if two different objects contain the same string.
The equals method
The Java continue statement causes execution to skip to
The next iteration in the loop
What Java keyword sometimes used in a class definition refers to the class itself?
This
A method can return only on value.
True
A void method does not contain a return statement, but it is possible.
True
An ArrayList class is used to store a mutable group of objects.
True
Coding a catch block for a parent exception type before the catch block of one of its subclasses causes a compilation error.
True
RunTimeException and its subclasses are unchecked exceptions.
True
When an object is passed to a method, the method receives a reference to the object.
True
int num = 12; System.out.println(num >= 4*3 && num % 2 ==0); What is the result of the code above?
True is printed
public static void main(String[] args) { System.out.print("W"; p(); System.out.print("Z"); } public static void p() { System.out.print("X"); System.out.print("Y"); } What is the output of the code above?
WXYZ
Identify the true statements. Select ALL that apply.
a. If errors that might occur can be handled with if blocks, exceptions should not be used. b. One finally block can follow the catch block(s). c. Code in a finally block is always executed. d. Exceptions in the Java API have constructors that can take a string argument that describes the error.
Where are Java used programs used? Select ALL that apply.
a. In some mobile devices b. On web servers c. In some modern motor vehicles d. In some kitchen appliances
public class Person { private String name; ... What term(s) describe(s) name in the snippet above? Select ALL that apply.
a. Instance variable b. Field c. Data member d. Attribute
Identify the data types that can be stored in a Java ArrayList. Select ALL that apply.
a. Integer b. Double c. String
Identify the statement(s) that compile without error(s). Select ALL that apply.
a. Integer num = 10; b. Integer num = new Integer(10); c. Integer num = new Integer("10");
Identify the true statements about Java. Select ALL that apply.
a. Java is case-sensitive b. A java program must have at least one class c. Java programs execute from the main method d. The name of a public Java Class must match its filename e. Java class names should begin with an upper case alphabet character
Identify the popular features of Java. Select ALL that apply.
a. Java is object-oriented. b. Java can be embedded in Web pages. c. Java is platform independent
int n1 = 12; int n2 = 5; Identify the statement(s) that are true. Select ALL that apply.
a. N2 > 10 ^ n1 > 10 b. N1 > 10 ^ n2 > 10
public class Person { private char gender; static int number; // ... public static void main(String[] args) { Person person // ... } public static void callPerson(int hours) { double payRate = 24.99; } } The code above compiles okay. Refer to it to complete this matching exercise.
a. Person: 1 b. Hours: 5 c. Gender: 5 d. payRate: 5 e. number: 2
Identify the data type(s) that can be used in a Java switch expression. Select ALL that apply.
a. String b. Short c. Int d. Char
Identify the true statements about Java strings. Select ALL that apply.
a. String is a reference type. b. Java strings are mutable. c. String is a class. d. A String object can be instantiated without using new. e. Strings once made cannot be made larger or smaller
Which of the following correctly assign the string hello to a String?
a. StringBuilder = str = new StringBuiler("hello"); String stt = str.toString(); b. String st = new String("hello"); c. String st = "hello"; d. char[] hello = {'h','e','l','l','o'}; String st = new String(hello);
Which statements about loops are true? Select ALL that apply.
a. The break keyword immediately stops a loop. b. Loops are good for processing arrays. c. A while loop might not run at all.
Identify the statement(s) that are true about arrays. Select ALL that apply.
a. The default value for the elements in an array of ints is zero. b. An array variable is a reference type. c. The default value for each element in an array of objects is null. d. Once created, an array cannot be re-sized.
Identify the true statement(s) about exceptions.
a. The finally block is optional b. Runtime exceptions are unchecked c. The root class of all exception classes is Throwable.
Identify the true statements. Select ALL that apply.
a. The keyword to throw an exception is throw, and the keyword to declare an exception is throws. b. The throws keyword indicates which exception(s) a method might throw. c. Every Java method must indicate the type(s) of exceptions that it might throw. d. One method might throw more than one type of exception.
String s1 = "Hello world"; String s2 = s1;
a. The s2 variable holds the string "Hello world". a. The s2 variable holds a reference to the string "Hello world".
Identify the true statements about overloaded methods. Select ALL that apply.
a. They must have different modifiers. b. They can have different parameter types. c. They must have the same method name. d. They can have a different number of parameters.
Which of the following are legal Java variable names? Select ALL that apply.
a. _name b. value c. $alary
Identify the expression(s) that return true if an int named num is between 100 and 200, inclusive. Select ALL that apply.\
a. num >= 100 && num <= 200 b. num > 99 && num < 201
int x=1; while(x <=5) { x++; System.out.print(x); } What is the output of the code above?
2 3 4 5 6
int num1 = 17, num2 = 5; System.out.print(num1 / num2); What is printed? Enter the output.
3
int x, y = 1; x = 10; if(x != 10 && x / 0 == 0) System.out.println(y); Else System.out.println(++y); What is the output of the code above?
2
double price = 14.95; System.out.printf("string",price); Which value for string will output three spaces followed by 14.95?
%8.2f
Which of the following is the correct expression that evaluates to true if the number x is between 1 and 100 or the number is negative?
((x < 100) && (x > 1)) || (x < 0)
int result = 3 / 10; What is the result of the code above?
0 is stored in result
public static void main(String[] args) { int number = 10; doubleNum(number); System.out.println(number); } public static void doubleNum(int n) { System.out.println(n * 2); } For the program above what will be the value displayed to the system console in the highlighted text?
10
Given the following method static void nPrint(String mmessage, int n) { while (n > 0) { System.out.print(message); n--; } } What is k after invoking nPrint("A message", k)? int k = 2; nPrint("A message", k);
2
What is the output of Integer.parseInt("10", 2)?
2
int var1 = 5, var2 = 6; if ((var2=1) == var1) System.out.print(var2); Else System.out.print(++var2); What is printed by the code above?
2
What is called by the throw statement?
A catch block.
What is returned by "AbE".compareToIgnoreCase("abC")?
A positive integer
Identify the statements that correctly increase the variable amount by 2. Select ALL that apply.
Amount += 2; Amount = amount + 2;
Select the statement that is false.
An array can be reset by clearing all elements.
Which of these technologies runs on the client side (in the browser)? Select ALL that apply.
Applets
6. int length = 5, width = 8; 7. area(length, width); As used in line 7, length and width are called the __?__ of the area method?
Arguments
__?__ is a binary relationship between two different classes.
Association
What term is used to describe compiled Java code?
Bytecode
Identify the valid statement(s). Select ALL that apply.
Char[] ch=new char[26]; b. double prices[] = {12.95,3.79,14.99,7.49};
Java forces you to deal with __?__ exceptions.
Checked
Identify the "blueprint" from which individual Java objects are created.
Class
The separation of how a class works internally from its usage is __?__
Class abstraction
An aggregating object exclusively own another object. This is __?__
Composition
Identify the components of object-oriented programming in Java. Select ALL that apply.
Encapsulation Inheritance Abstraction Polymorphism
Boolean odd = 12 % 2 == 1; If (odd = false) System.out.println("odd"); Else System.out.println("Even"); What is printed (if anything) by the code above?
Even
int b =0, sum =0; while (b < 6) { ++b; if(b % 2 == 0) continue; b++; sum = sum + b; } System.out.print(sum); When the above program is run, the output would be 13?
False
To declare a constant MAX_LENGTH as a member of the class, you write _____.
Final static double MAX_LENGTH = 99.98;
Identify the statement that will generate a compile error.
Float price = 4.99;
Public static double area(double radius){ ... In the method header above, radius is called a __?__
Formal parameter
If no exception occurs in a try-catch block, the code in the finally clause ______.
Is executed
What modifier should you use on the members of a class so that they are not accessible to another class in a different package, but are accessible to any subclasses in any package?
protected
In which instance can a loop not require braces?
When only one statement is to be repeated
Identify the logical operator(s). Select ALL that apply.
a. ! b. ^ c. &&
Match the format specifiers with the data.
a. %d - integer b. %e - scientific notation c. %f - floating point d. %c - character e. %b - Boolean f. %s - string
Int x = 5; Which statement(s) will make x equal to 6? Select ALL that apply.
a. ++x; b. x = ++x; c. x = x + 1;
Identify the correct use of comments. Select ALL that apply.
a. // this is a Java comment b. System.out.println("Hello, World"); // say Hello c. /* This is a Java comment */
1. Identify the relational operators. Select ALL that apply.
a. <= b. == c. >
Identify the correct statements. Select ALL that apply.
a. A Java string is enclosed by double quotes b. Java programs start in the main method. c. The contents of a class are enclosed by curly braces () d. A compiled Java filename has a .class extension.
Identify the true statements. Select ALL that apply.
a. A getter method has the same return-type as the field it retrieves and takes no parameters. b. A setter method is a return-type void and takes a parameter of the same type as its field. c. Setters and getters are not required for public instance variables.
Identify the valid statement(s). Select ALL that apply.
a. A java class can extend only one subclass. b. Nonstatic methods cannot be called from within static methods c. In Java, an array variable is a reference type d. An Arraylist cannot store primitive types.
Identify the true statements. Select ALL that apply.
a. A static method cannot access the data members of its own class. b. Static methods are also called class methods. c. Static methods of a class can be called without instantiating the class.
Identify the true statements regarding Java loops. Select ALL that apply.
a. A while loop might not run at all. b. A do-while loop always runs at least once. c. Loops can be nested in other loops to any depth as desired.
Identify the true statements. Select ALL that apply.
a. Anonymous objects are possible in Java. b. Constants in a class should be declared as final static. c. Static variable and static methods of a class can be accessed by an object of that class and by the class name.
Select all the valid examples of a runtime exception messages.
a. ArithmeticException b. NullPointerException c. IndexOutOfBoundsException
Identify the statements that will NOT compile. Select ALL that apply.
a. ArrayList[String] stock = new ArrayList[String](); b. ArrayList<char> charas = new ArrayList<char>(); c. Int[4] items = {5,9,11,8}
Identify the legal Java identifiers. Select ALL that apply.
a. CanOfWorms b. _firstname c. $price
Identify the true statements. Select ALL that apply.
a. Class Throwable is the parent class for all exceptions. b. You can create your own exception classes by extending class Exception.
Identify the true statements. Select ALL that apply.
a. Constructors are called with the new operator. b. Constructors can be overloaded. c. Constructors must have the same name as the class.
Identify the true statements. Select ALL that apply.
a. Elements can be appended to an existing ArrayList instance. b. Arrays can store primitive types and reference types. c. Arraylists cannot store primitive data types.
Identify the true statements about defining custom data classes. Select ALL that apply.
a. Generally, code data as private and methods as public. b. String data fields are assigned a default value of null. c. Numeric data fields are assigned zero by default. d. Constructors have no return type.
String str2 = "b$h&".replaceAll("@&$","oo"); System.out.println(str2.replaceAll("[@&$]","oo")); What is printed?
boohoo
An object is an instance of a ______.
class
Identify the correct Java statement(s). Select ALL that apply.
float taxRate = 0.07F; float taxRate = 0.07f;
Identify the correct Java variable assignment statements. Select ALL that apply.
int bin = 0b111; double cost = 2_345.5_; double num - 1_00_0.0_0;
Source code file Hello.java has been compiled. Which one of these will execute it?
java Hello
Identify the Java package that is never imported.
java.lang
Which package must be imported to create an instance of the Scanner class?
java.util
The JDK command to compile the file Test.java is ______.
javac Test.java
Which one will compile a Java source code file named Test.java?
javac Test.java
int n1 = 12; int n2 = 5; int n3 = ++n1 - n1--; What is the result of the code above?
n3 = 8 and n2 = 4 and n1 = 13
Coding a method in a subclass with the same signature and return type as a method in its superclass is called _______.
overriding
Simple data types for storing integers, real numbers, characters and Booleans are called ____?____ in Java.
primitives
Which of these operators might not have to evaluate the right hand operand?
||