Java Fundamentals - 1Z0-811

Ace your homework & exams now with Quizwiz!

The program below has a syntax error. class ExamPrep { public static void main(String[] args) { System.out.println("Hello Java") } } It is missing a semicolon on line 3. What will happen when you compile and run this? Choose the correct answer: A. When you execute javac hello.java, the command will fail and an error will be printed showing that ';' is expected on line 3. B. javac hello.java will complete without an error, but when you execute java ExamPrep, the command will fail and an error will be printed showing that ';' is expected on line 3. C. javac hello.java will complete without an error, but when you execute java ExamPrep, a runtime exception exception will occur showing that ';' is expected on line 3. D. javac hello.java will complete without an error, and java ExamPrep will print Hello Java.

A. When you execute javac hello.java, the command will fail and an error will be printed showing that ';' is expected on line 3.

You have a variable named a that is an int array of size 4, defined like this: int a[] = new int[4]; How can you assign the value 99 to the first element in the array? Choose the correct answer: A. a[0] = 99; B. a[1] = 99; C. a(0) = 99; D. a(1) = 99;.

A. a[0] = 99;

Which of the following can be defined using the private modifier? Choose 4 correct answers: A. class B. method C. class variable D. instance variable E. local variable

A. class B. method C. class variable D. instance variable

Which type of variable has its value shared by every instance of the class? Choose the correct answer: A. class variable B. instance variable C. local variable D. None of these

A. class variable

Which type of variable is defined using the static keyword? Choose the correct answer: A. class variable B. instance variable C. local variable D. None of these

A. class variable

Following the Java naming conventions, which of the following should begin with an upper case letter? Choose 2 correct answers: A. classes B. interfaces C. local variables D. packages E. private methods F. public methods

A. classes B. interfaces

You have an Arraylist variable named a of type ArrayList<String>. Which of the following code snippets will print all of the elements in the list? Choose all of the correct answers. Each answer represents a complete solution. A. for (String s : a) { System.out.println(s); } B. for (int i = 0; i < a.size(); i++) { System.out.println(a.get(i)); } C. Iterator i = a.iterator(); while (i.hasNext()) { System.out.println(i.next()); }

A. for (String s : a) { System.out.println(s); } B. for (int i = 0; i < a.size(); i++) { System.out.println(a.get(i)); } C. Iterator i = a.iterator(); while (i.hasNext()) { System.out.println(i.next()); } All of these are different ways to accomplish the same thing

Which java loop type(s) allow you to iterate over elements of an array without using an index variable? Choose all of the correct answers. A. for loop B. while loop C. do while loop D. None of these

A. for loop

Which java loop type(s) evaluates the conditional expression BEFORE executing each iteration of the code within the body of the loop? Choose all of the correct answers. A. for loop B. while loop C. do while loop D. None of these

A. for loop B. while loop

Which code block will print No if the value of a and b are not equal? Choose the correct answer: A. if (a != b) { System.out.println("No"); } B. if (a !== b) { System.out.println("No"); } C. if (a !=== b) { System.out.println("No"); } D. if (a ! b) { System.out.println("No"); }

A. if (a != b) { System.out.println("No"); }

You are writing a program to generate a random number between 0 and 1, and if it is less than .5, print heads, otherwise print tails. You have the following code so far: import java.lang.Math; class ExamPrep { public static void main(String[] args){ double result = Math.random(); // LINE 6 } } What can you add at LINE 6 to print heads or tails depending on the value of result? Choose the correct answer: A. if (result < .5) { System.out.println("heads"); } else { System.out.println("tails"); } B. if (result < .5) System.out.println("heads"); otherwise System.out.println("tails"); C. if (result < .5) { System.out.println("heads"); } or else { System.out.println("tails"); } D. if (result < .5) System.out.println("heads"); or else System.out.println("tails");

A. if (result < .5) { System.out.println("heads"); } else { System.out.println("tails"); }

You are writing a Java program and need to declare a variable called myvar that can store an integer value, and you also want to assign to it the value of 5. Which line of code should you use? Choose the correct answer: A. int myvar = 5; B. integer myvar = 5; C. myvar int = 5; D. myvar integer = 5;

A. int myvar = 5;

Which of the following lines of code will get the absolute value of a variable a? Choose the correct answer: A. int result = java.lang.Math.abs(x); B. int result = abs(x); C. int result = java.lang.Math.absoluteValue(x); D. int result = absoluteValue(x); E. int result = !x;

A. int result = java.lang.Math.abs(x)

You have created a simple Java program in a file called hello.java with the following code: class ExamPrep { public static void main(String[] args){ System.out.println("Hello Java"); } } You executed javac hello.java and see that it produced a file called ExamPrep.class in the same directory. Which command can you use to execute the program and see the output of Hello Java? Choose the correct answer: A. java ExamPrep B. java ExamPrep.class C. javac ExamPrep D. javac ExamPrep.class

A. java ExamPrep

Which method of the String class returns the length of the string? Choose the correct answer: A. length() B. size() C. capacity() D. getLength() E. getSize() F. getCapacity()

A. length()

Which of these functions will compare two integers, a and b, and print THEY ARE EQUAL if they are equal? Choose 2 correct answers: A. public void printComparison(int a, int b) { if (a == b) { System.out.println("THEY ARE EQUAL"); } } B. public void printComparison(int a, int b) { if (a.equals(b)) { System.out.println("THEY ARE EQUAL"); } } C. public void printComparison(Integer a, Integer b) { if (a.equals(b)) { System.out.println("THEY ARE EQUAL"); } } D. public void printComparison(int a, int b) { if (Integer.equals(a, b)) { System.out.println("THEY ARE EQUAL"); } } E. public void printComparison(int a, int b) { if (a instanceof b) { System.out.println("THEY ARE EQUAL"); } }

A. public void printComparison(int a, int b) { if (a == b) { System.out.println("THEY ARE EQUAL"); } } C. public void printComparison(Integer a, Integer b) { if (a.equals(b)) { System.out.println("THEY ARE EQUAL"); } }

What is the output of the following program? class ExamPrep { public static void main(String[] args){ int a = 1; int b = 2; int c = 3; boolean result = a < b || b > c; System.out.println(result); } } Choose the correct answer: A. true B. false C. This code will not compile D. This code will fail with an exception at runtime

A. true

Which type of application cannot be created using Java? A. Desktop Application B. Web Application C. Mobile Application D. None of these E. All of These

All of these can be used to create Java applications

Which of the following statements are TRUE about objects in Object-Oriented Programming? Choose 2 correct answers: A. Objects should only be used to represent real-world physical objects B. Objects use fields to store state C. Objects provide methods to operate on its internal state D. Objects optimize performance of your code

B and C

What is the output of the following program? class ExamPrep { public static void main(String[] args){ int a = 4; int b = 3; int result = a < b ? 1 : 2; System.out.println(result); } } Choose the correct answer: A. 1 B. 2 C. 3 D. 4 E. This code will not compile F. This code will fail with an exception at runtime

B. 2

You run the following program: import java.util.Random; class ExamPrep { public static void main(String[] args){ Random r = new Random(); int a = r.nextInt(100); int b = r.nextInt(100); int c = r.nextInt(100); System.out.printf("%d,%d,%d%n", a, b, c); } } The output is 27,48,83. What will the output be if you run it again? Choose the correct answer: A. 27,48,83 B. A different set of three random numbers between 0 (inclusive) and 100 (exclusive) C. This code will not compile D. This code will fail with an exception at runtime

B. A different set of three random numbers between 0 (inclusive) and 100 (exclusive) CORRECT When called without a seed, Random's constructor will choose a unique seed, resulting in a different different order of pseudorandom values to be returned each time the code is run.

Which of the following statements are TRUE about Java Arrays and ArrayLists? Choose 3 correct answers: A. Arrays can be resized B. ArrayLists can be resized C. Arrays can store either primitives or objects D. ArrayLists can store either primitives or objects E. ArrayList implements the List interface F. Array implements the List interface

B. ArrayLists can be resized C. Arrays can store either primitives or objects E. ArrayList implements the List interface

You are working on a Java project and see the following code: class Customer { private int id; private String name; public int getId() { return id; } public void setId(int value) { id = value; } public String getName() { return name; } public void setName(String value) { name = value; } } How can you create an instance of the Customer class with and ID of 12 and Name of ACME? Choose the correct answer: A. Customer c = new Customer(12, "ACME"); B. Customer c = new Customer(); c.setId(12); c.setName("ACME"); C. Customer c = new Customer(); c.id = 12; c.name = "ACME"; D. You cannot create an instance of this class because no constructor has been declared

B. Customer c = new Customer(); c.setId(12); c.setName("ACME");

TRUE or FALSE: The random() method of java.lang.Math returns an integer value between 0 (inclusive) and 100 (exclusive)? Choose the correct answer: A. TRUE B. FALSE

B. FALSE - java.lang.Math.random() returns a double between 0.0 (inclusive) and 1.0 (exclusive). The first time it is called, it creates a new pseudorandom-number generator, using new java.util.Random() and each subsequent call uses this instance of the Random class internally.

You are working on a project that has an existing function called getNextId() which returns an int. You are given the following code: while (true) { int id = getNextId(); if (id == -1) { break; } System.out.println(id); } System.out.println("Done"); Which of the following statements are TRUE about this code? Choose the correct answer: A. There is no way the code will exit from the loop, and Done will never be printed because the condition is while(true). B. If getNextId() returns -1, it will exit the loop (or "break" out of the loop) and print Done. C. If getNextId() returns -1, it will throw an exception on the line that says break and Done will not be printed. D. This code will not compile because of the infinite loop

B. If getNextId() returns -1, it will exit the loop (or "break" out of the loop) and print Done.

You are writing a function and need to declare another variable. Which of these variable declarations will compile without an error? Choose the correct answer: A. String 200 = "Two Hundred"; B. String $200 = "Two Hundred"; C. String 200$ = "Two Hundred"; D. None of these are valid

B. String $200 = "Two Hundred";

You are writing a function and need to declare a variable. Which of these variable declarations will compile without an error? Choose the correct answer: A. String 100 = "One Hundred"; B. String _100 = "One Hundred"; C. String 100_ = "One Hundred"; D. None of these are valid

B. String _100 = "One Hundred";

What can you add at LINE 6 so that the program will print Java Exam Prep? class ExamPrep { public static void main(String[] args){ String a = "Java"; String b = "Exam"; String c = "Prep"; // LINE 6 System.out.println(result); } } Choose the correct answer: A. String result = String.format("%d %d %d", a, b, c); B. String result = String.format("%s %s %s", a, b, c); C. String result = String.format("%n %n %n", a, b, c); D. String result = String.format("%a %b %c");

B. String result = String.format("%s %s %s", a, b, c);

Which of the following lines of Java code can you use to declare and initialize a string variable? Choose 2 correct answers: A. string s = "Hello Java"; B. String s = "Hello Java"; C. String s = new String("Hello Java"); D. string s = String("Hello Java");

B. String s = "Hello Java"; C. String s = new String("Hello Java");

What is the output of the following program? class ExamPrep { public static void main(String[] args){ String a = new String("Java"); String b = new String("Java"); if (a == b) { System.out.println("They are equal"); } else { System.out.println("They are not equal"); } } } Choose the correct answer: A. They are equal B. They are not equal C. This program will not compile D. This program will fail with an exception at runtime

B. They are not equal - == is a reference comparison, so since a and b do not share the same reference, == will return false

Which of the following are primitive datatypes in Java? Choose 5 correct answers: A. bool B. boolean C. byte D. char E. date F. double G. int H. single I. string

B. boolean C. byte D. char F. double G. int

You have the following interface: interface Bicycle { void changeCadence(int newValue); void changeGear(int newValue); void speedUp(int increment); void applyBrakes(int decrement); } How can you define a class that can be used wherever this interface is expected? Choose the correct answer: A. class ACMEBicycle extends Bicycle { } B. class ACMEBicycle implements Bicycle { } C. class ACMEBicycle : Bicycle { } D. class ACMEBicycle { }

B. class ACMEBicycle implements Bicycle { }

Which of the following will print Hello Java? A. class ExamPrep { public void main(String[] args){ System.out.println("Hello Java"); } } B. class ExamPrep { public static void main(String[] args){ System.out.println("Hello Java"); } } C. class ExamPrep { public void Main(String[] args){ System.out.println("Hello Java"); } } D. class ExamPrep { public static void Main(String[] args){ System.out.println("Hello Java"); } }

B. class ExamPrep { public static void main(String[] args){ System.out.println("Hello Java"); } }

What is the output of the following program? class ExamPrep { public static void main(String[] args){ int a = 1; int b = 2; int c = 3; boolean result = a < b && b > c; System.out.println(result); } } Choose the correct answer: A. true B. false C. This code will not compile D. This code will fail with an exception at runtime

B. false

What is the output of the following program? class ExamPrep { public static void main(String[] args){ String s = "Java Exam Prep"; boolean result = s.contains("exam"); System.out.println(result); } } Choose the correct answer: A. true B. false C. -1 D. 4 E. This program will not compile F. This program will fail with an exception at runtime

B. false -Remember: String comparisons are case-sensitive!

You are working on a Java project and see the following code: class Customer { private int id; private String name; } What does the private modifier do? Choose the correct answer: A. id and name are encrypted. B. id and name can only be accessed by methods defined inside the Customer class. C. id and name can only be accessed by methods defined inside the Customer class or inside any class that inherits from Customer. D. id and name can only be accessed by methods defined inside the Customer class or inside any class that is defined within the same package.

B. id and name can only be accessed by methods defined inside the Customer class.

Which code block will print Yes if the value of a and b are equal? Choose the correct answer: A. if (a = b) { System.out.println("Yes"); } B. if (a == b) { System.out.println("Yes"); } C. if (a === b) { System.out.println("Yes"); } D. if (a instanceof b) { System.out.println("Yes"); }

B. if (a == b) { System.out.println("Yes"); }

The following program has a bug. You were expecting the output to be They are equal, but it was printing They are not equal instead. class ExamPrep { public static void main(String[] args){ String a = new String("Java"); String b = new String("Java"); if (a == b) { // LINE 5 System.out.println("They are equal"); } else { System.out.println("They are not equal"); } } } What can you replace LINE 5 with to fix the problem? Choose the correct answer: A. if (a instanceof b) { B. if (a.equals(b)) { C. if (a === b) { D. if (a is b) {

B. if (a.equals(b)) {

You are writing a Java function that has the following code: short a = 1; short b = 2; // LINE 3 What can you put at // LINE 3 to add the two variables a and b and store the result in another variable c? Choose 2 correct answers: A. short c = a + b; B. int c = a + b; C. short c = (short)(a + b); D. short c = (short)a + (short)b;

B. int c = a + b; C. short c = (short)(a + b);

Which of the following code snippets will print the numbers 1 through 10? Choose the correct answer: A. int i = 0; while (i <= 10) { System.out.println(i); i++; } B. int i = 1; while (i <= 10) { System.out.println(i); i++; } C. int i = 0; while (i < 10) { System.out.println(i); } D. int i = 1; while (i < 11) { System.out.println(i); }

B. int i = 1; while (i <= 10) { System.out.println(i); i++; }

What can you put at line 4 so that the output of the program will be 10? class ExamPrep { public static void main(String[] args){ int a = 3; // LINE 4 System.out.println(result); } } Choose the correct answer: A. int result = a + 2 * a - 1; B. int result = (a + 2) * (a - 1); C. int result = (a + 2 * a) - 1; D. int result = a + (2 * a - 1);.

B. int result = (a + 2) * (a - 1);

What is the correct way to declare a variable named a that is an int array of size 4? Choose the correct answer: A. int a[] = new int[4]; B. int[] a = new int[4]; C. int a[] = new int(4); D. int[] a = new int(4);

B. int[] a = new int[4];

Which of these is NOT a Java reserved word? Choose the correct answer: A. default B. object C. import D. new E. All of these are Java reserved words

B. object

You have defined a custom class called Customer with the following code: public class Customer { private int id; private String name; public Customer(int id, String name) { this.id = id; this.name = name; } public boolean equals(Object o) { if (o == this) return true; else if (o == null || !(o instanceof Customer)) return false; Customer c = (Customer)o; return Objects.equals(id, c.id) && Objects.equals(name, c.name); } } Two customers are considered equal if they have the same id and name. How can you compare two customer objects, a and b, and print THEY ARE EQUAL if they are equal? Choose the correct answer: A. public void printComparison(Customer a, Customer b) { if (a == b) { System.out.println("THEY ARE EQUAL"); } } B. public void printComparison(Customer a, Customer b) { if (a.equals(b)) { System.out.println("THEY ARE EQUAL"); } } C. public void printComparison(Customer a, Customer b) { if (Customer.equals(a, b)) { System.out.println("THEY ARE EQUAL"); } } D. public void printComparison(Customer a, Customer b) { if (a instanceof b) { System.out.println("THEY ARE EQUAL"); } }

B. public void printComparison(Customer a, Customer b) { if (a.equals(b)) { System.out.println("THEY ARE EQUAL"); } }

You have written the following function to add two numbers: public int add(int a, int b) { return a + b; } Now, you want to add a comment saying Add two numbers on the same line as the return statement. How can you do this? Choose 2 correct answers: A. return a + b; # Add two numbers B. return a + b; /* Add two numbers */ C. return a + b; -- Add two numbers D. return a + b; // Add two numbers

B. return a + b; /* Add two numbers */ D. return a + b; // Add two numbers

Which method of the String class returns a copy of the string convered to lowercase? Choose the correct answer: A. toLower() B. toLowerCase() C. asLower() D. asLowerCase() E. lower()

B. toLowerCase()

Which operator is Java's modulo operator, returning the remainder after dividing the first operand by the second operand? Choose the correct answer: A. / B. \ C. % D. ^

C. %

What is the output of the following program? class ExamPrep { public static void main(String[] args){ String s = "Java"; int result = s.indexOf("a"); System.out.println(result); } } Choose the correct answer: A. -1 B. 0 C. 1 D. 2 E. 3 F. 4 G. This program will not compile H. This program will fail with an exception at runtime

C. 1

What will be the output of the following Java program? class ExamPrep { public static void main(String[] args){ int a = 5; int b = ++a; System.out.println(b); } } Choose the correct answer: A. 4 B. 5 C. 6 D. This program will not compile E. This program will fail with a runtime error

C. 6. a is incremented before its value is assigned to b

What will be the output of the following Java program? class ExamPrep { public static void main(String[] args){ int a = 5; int b = a++; System.out.println(b); } } Choose the correct answer: A. 4 B. 5 C. 6 D. This program will not compile E. This program will fail with a runtime error

C. 6. a is incremented before its value is assigned to b

The following lines are part of the code in your program: String a = new String("Java!"); String b = new String("Exam."); int result = a.compareTo(b); You have determined that the value of result is 5. What does this mean? Choose the correct answer: A. The String value stored in a is 5 characters different than the String value stored in b. B. The String value stored in a is lexographically less than the String value stored in b. C. The String value stored in a is lexographically greater than the String value stored in b. D. Nothing. The compareTo() method does not apply to strings and the return value is inconsistent.

C. The String value stored in a is lexographically greater than the String value stored in b. compareTo compares two strings lexicographically. It returns an integer indicating whether the string is greater than (result is less than 0), equal to (result equals 0), or less than (result is greater than 0) the argument.

You have the following code: import java.util.*; class ExamPrep { public static void main(String[] args) { ArrayList<String> a = new ArrayList<String>(); // LINE 6 } } What can you add at LINE 6 to add "Java" as the first element in the ArrayList? Choose the correct answer: A. a[0] = "Java"; B. a[1] = "Java"; C. a.add("Java"); D. a.insert("Java");

C. a.add("Java");

You have the following code: import java.util.*; class ExamPrep { public static void main(String[] args) { ArrayList<String> a = new ArrayList<String>(); a.add("Java"); // LINE 7 } } What can you add at LINE 7 to insert "First" as the first element in the ArrayList? Choose the correct answer: A. a.add("First"); B. a.insert("First"); C. a.add(0, "First"); D. a.insert(0, "First");

C. a.add(0, "First");

Which of the following lines of code can be used to determine if the content of a String variable s starts with "java"? Choose the correct answer: A. boolean result = s.beginsWith("java"); B. boolean result = s.hasPrefix("java"); C. boolean result = s.startsWith("java"); D. boolean result = s.prefixEquals("java");

C. boolean result = s.startsWith("java");

You have the following code. class ExamPrep { public static void main(String[] args){ String s = "Java"; // LINE 4 System.out.println(result); } } What can you put at line 4 so that the output of the program will be v? Choose the correct answer: A. char result = s[2]; B. char result = s[3]; C. char result = s.charAt(2); D. char result = s.charAt(3);

C. char result = s.charAt(2);

Customer is a class that has a method getName() and a constructor that takes the name as an argument. You have an array of Customer objects, defined like this: Customer[] customers = { new Customer("a"), new Customer("b"), new Customer("c") }; Which code snippet can be used to print the name of all customers in the customers array? Choose the correct answer: A. for (Customer c in customers) { System.out.println(c.getName()); } B. for each (Customer c in customers) { System.out.println(c.getName()); } C. for (Customer c : customers) { System.out.println(c.getName()); } D. for each (Customer c : customers) { System.out.println(c.getName()); }

C. for (Customer c : customers) { System.out.println(c.getName()); }

You are writing a program to generate a random number between 0 and 1, and if it is less than .5, print less than half. You have the following code so far: import java.lang.Math; class ExamPrep { public static void main(String[] args){ double result = Math.random(); // LINE 6 } } What can you add at LINE 6 to print less than half when the random number is less than .5? Choose 2 correct answers. Each answer represents a complete solution. A. if result < .5 { System.out.println("less than half"); } B. if result < .5 then { System.out.println("less than half"); } C. if (result < .5) { System.out.println("less than half"); } D. if (result < .5) then { System.out.println("less than half"); } E. if (result < .5) System.out.println("less than half");

C. if (result < .5) { System.out.println("less than half"); } E. if (result < .5) System.out.println("less than half");

You have written the following code: package com.example; // LINE 2 public class Main { public static void main(String[] args) { Rectangle myRectangle = new Rectangle(); } } When you compile it, you get an error saying java: cannot find symbol. What can you add at // LINE 2 to fix this problem? Choose the correct answer: A. class graphics.Rectangle; B. export graphics.Rectangle; C. import graphics.Rectangle; D. package graphics.Rectangle;

C. import graphics.Rectangle;

Which static function on java.lang.Math can you use to return the smaller of two values? Choose the correct answer: A. floor B. lessThan C. min D. smallerOf.

C. min

You need to store the value of 128 in a variable. Which data type can be used? Choose all of the correct answers. A. boolean B. byte C. short D. int E. long

C. short D. int E. long

What will be the output of the following code? class ExamPrep { public static void main(String[] args) { try { doSomething(); System.out.println("Success"); } catch (Exception e) { System.err.println("Exception"); } finally { System.out.println("Finally"); } } static void doSomething() throws Exception { throw new Exception("Something unexpected happened"); } } Choose the correct answer: A. Success Exception Finally B. Success Finally Exception C.Exception Finally D. Finally Exception E. Exception

C.Exception Finally

You are working on a Java project that has the following code: class Customer { private static int customerCount = 0; private int id; private String name; public int getId() { return id; } public String getName() { return name; } public int getCustomerCount() { return customerCount; } public Customer(int startId, String startName) { id = startId; name = startName; customerCount++; } } You create a couple of Customer instances, and then print the output of getCustomerCount() for both instances, like this: class ExamPrep { public static void main(String[] args) { Customer a = new Customer(1, "cust1"); Customer b = new Customer(2, "cust2"); System.out.println(a.getCustomerCount()); System.out.println(b.getCustomerCount()); }} What will the output be when you run this program? Choose the correct answer: A. 0 1 B. 1 1 C. 1 2 D. 2 2

D. 2 2

You have the following code: import java.util.*; class ExamPrep { public static void main(String[] args) { // LINE 5 } } What can you add at LINE 5 to declare a variable named a which is an ArrayList of Strings? Choose the correct answer: A. List(String) a = new List(String)(); B. List<String> a = new List<String>(); C. ArrayList(String) a = new ArrayList(String)(); D. ArrayList<String> a = new ArrayList<String>();

D. ArrayList<String> a = new ArrayList<String>();

Consider the following code snippit: class MountainBike extends Bicycle { } Which Object-Oriented Programming concept is this an example of? A. Composition B. Encapsulation C. Idempotency D. Inheritance

D. Inheritance

What can you add at LINE 4 to make the program print Percent Complete = 56.79? class ExamPrep { public static void main(String[] args){ double a = 56.785; // LINE 4 System.out.println(result); } } Choose the correct answer: A. String result = String.format("Percent Complete = %d", a); B. String result = String.format("Percent Complete = %.2d", a); C. String result = String.format("Percent Complete = %f", a); D. String result = String.format("Percent Complete = %.2f", a); E. String result = String.format("Percent Complete = %s", a); F. String result = String.format("Percent Complete = %.2s", a);

D. String result = String.format("Percent Complete = %.2f", a);

You have the following code. class ExamPrep { public static void main(String[] args){ String s = "Java"; // LINE 4 System.out.println(result); } } What can you put at line 4 so that the output of the program will be av? Choose the correct answer: A. String result = s.split(1, 2); B. String result = s.split(1, 3); C. String result = s.substring(1, 2); D. String result = s.substring(1, 3);

D. String result = s.substring(1, 3);

You have written the following Java program in a file called hello.java: class ExamPrep { public static void main(String[] args){ int myvar; System.out.println(myvar); } } You run javac hello.java and java ExamPrep. What is the output? Choose the correct answer: A. 0 B. null C. undefined D. The javac hello.java command fails with an error because you have not initialized myvar.

D. The javac hello.java command fails with an error because you have not initialized myvar.

Which of the following lists arrange data types from smallest capacity to largest capacity? Choose the correct answer: A. short, byte, int, long B. byte, short, long, int C. byte, int, short, long D. byte, short, int, long

D. byte, short, int, long

Which program will compile without error and print Hello Java when it is run? Choose the correct answer: A. class ExamPrep { public void main() { System.out.println("Hello Java"); } } B. class ExamPrep { public void main(String[] args) { System.out.println("Hello Java"); } } C. class ExamPrep { public static void main() { System.out.println("Hello Java"); } } D. class ExamPrep { public static void main(String[] args) { System.out.println("Hello Java");}}

D. class ExamPrep { public static void main(String[] args) { System.out.println("Hello Java");}}

Which of the following code snippets will print the numbers 1 through 10? Choose the correct answer: A. for (int i = 0; i < 10; ++i) { System.out.println(i); } B. for (int i = 0; i < 11; ++i) { System.out.println(i); } C. for (int i = 1; i < 10; i++) { System.out.println(i); } D. for (int i = 1; i < 11; i++) { System.out.println(i); }

D. for (int i = 1; i < 11; i++) { System.out.println(i); }

Which of the following code snippets will print the numbers 1 through 10? Choose the correct answer: A. int i = 0; do { System.out.println(i); i++; } until (i == 10); B. int i = 1; do { System.out.println(i); i++; } until (i == 10); C. int i = 0; do { System.out.println(i); i++; } while (i < 11); D. int i = 1; do { System.out.println(i); i++; } while (i < 11);

D. int i = 1; do { System.out.println(i); i++; } while (i < 11);

You have created a simple Java program in a file called hello.java with the following code: class ExamPrep { public static void main(String[] args){ System.out.println("Hello Java"); } } Which command can be used to compile this program, converting it into bytecodes, which are platform-independent instructions for the Java VM? Choose the correct answer: A. java hello.java B. java -c hello.java C. java -jar hello.java D. javac hello.java

D. javac hello.java

You are a software developer for Example Corp where you are creating a new Java package. You need to come up with a name for the new package. Which line of code follows the Java convention for naming packages? Choose the correct answer: A. package ExampleCorp; B. package exampleCorp; C. package corpExample; D. package com.example; E. package example.com; F. package www.example.com;

D. package com.example;

You have the following code. class ExamPrep { public static void main(String[] args){ int a = 5; // LINE 4 } static void printMessage(String message) { System.out.println("Message = " + message); } } What can you put at line 4 so that the output of the program will be Message = 5? Choose 2 correct answers. Each answer represents a complete solution. A. printMessage(a); B. printMessage(a.toString()); C. printMessage((String)a)); D. printMessage(String.valueOf(a)); E. printMessage(Integer.toString(a));

D. printMessage(String.valueOf(a)); E. printMessage(Integer.toString(a));

You have the following program in a file called hello.java that reads the contents of a file called data.txt and prints the number of lines contained within it. import java.nio.file.*; import java.util.List; class ExamPrep { public static void main(String[] args) { Path path = Path.of("data.txt"); List<String> lines = Files.readAllLines(path); System.out.println(lines.size()); } } When you compile this program using javac hello.java, you get the following error: hello.java:7: error: unreported exception IOException; must be caught or declared to be thrown List<String> lines = Files.readAllLines(path); ^ 1 error You have decided to add a try/catch block to fix this error. Which code block below shows a syntactically correct use of a try/catch block and will compile without error? A. public static void main(String[] args) { Path path = Path.of("data.txt"); try { List<String> lines = Files.readAllLines(path); } catch { System.out.println(java.io.IOException.getMessage()); } System.out.println(lines.size()); } B. public static void main(String[] args) { Path path = Path.of("data.txt"); try { List<String> lines = Files.readAllLines(path); System.out.println(lines.size()); } catch { System.out.println(java.io.IOException.getMessage()); } } C. public static void main(String[] args) { Path path = Path.of("data.txt"); try { List<String> lines = Files.readAllLines(path); } catch (java.io.IOException e) { System.out.println(e.getMessage()); } System.out.println(lines.size()); } D. public static void main(String[] args) { Path path = Path.of("data.txt"); try { List<String> lines = Files.readAllLines(path); System.out.println(lines.size()); } catch (java.io.IOException e) { System.out.println(e.getMessage()); } }

D. public static void main(String[] args) { Path path = Path.of("data.txt"); try { List<String> lines = Files.readAllLines(path); System.out.println(lines.size()); } catch (java.io.IOException e) { System.out.println(e.getMessage()); } }

Which ArrayList method returns the number of elements in the list? Choose the correct answer: A. capacity() B. count() C. length() D. size()

D. size()

Which method of the String class returns a copy of the string with leading and trailing white space removed? Choose the correct answer: A. removeSpace() B. removeWhiteSpace() C. split() D. trim() E. withoutSpace()

D. trim()

What is the output of the following program? class ExamPrep { public static void main(String[] args){ String s = "Java"; int result = s.lastIndexOf("a"); System.out.println(result); } } Choose the correct answer: A. -1 B. 0 C. 1 D. 2 E. 3 F. 4 G. This program will not compile H. This program will fail with an exception at runtime.

E. 3

Which of the following is NOT an operator in Java? Choose the correct answer: A. + B. - C. * D. / E. \ F. %

E. \

You have the following code: import java.util.*; class ExamPrep { public static void main(String[] args) { ArrayList<String> a = new ArrayList<String>(); a.add("Java"); a.add(0, "First"); // LINE 8 } } What can you add at LINE 8 to remove "First" from the ArrayList? Choose 2 correct answers. Each answer represents a complete solution. A. a.delete("First"); B. a.delete(0); C. a.deleteAt(0); D. a.remove("First"); E. a.remove(0); F. a.removeAt(0);

E. a.remove(0);

You have written the following Java program: class ExamPrep { public static void main(String[] args){ printColor(2); } private static void printColor(int x) { switch (x) { case 1: System.out.println("red"); case 2: System.out.println("blue"); case 3: System.out.println("green"); default: System.out.println("something else"); } } } What will the output be when you run this program? Choose the correct answer: A. red B. blue C. green D. something else E. blue, green, something else

E. blue, green, something else - You need to use the break keyword within each case if you want to prevent fall-through and resume execution outside of the switch statement.

You have the following Java program: class ExamPrep { public static void main(String[] args){ double a = 5.0; double b = 2.0; // LINE 5 System.out.println(result); } } What can you put at LINE 5 to get the result of a raised to the power of b? Choose the correct answer: A. double result = a ^ b; B. double result = b ^ a; C. double result = java.lang.Math.exp(a, b); D. double result = java.lang.Math.exp(b, a); E. double result = java.lang.Math.pow(a, b); F. double result = java.lang.Math.pow(b, a);

E. double result = java.lang.Math.pow(a, b);

The __________ includes the JRE plus command-line development tools such as compilers and debuggers that are necessary or useful for developing applets and applications.

Java Development Kit (JDK)

The __________ provides the libraries, Java virtual machine, and other components necessary for you to run applets and applications written in the Java programming language. It can be redistributed with applications to make them free-standing.

Java Runtime Environment (JRE)

A __________ is a namespace for organizing classes and interfaces in a logical manner.

Package

You have a variable a defined like this: int a = 5; Which lines of code can you use to decrement a by 1? Choose all of the correct answers. Each answer represents a complete solution. A. --a; B. a--; C. a -= 1; D. a =- 1; E. a = a - 1;

A, B, C, E

What is the output of the following program? class ExamPrep { public static void main(String[] args){ String s = "Java"; int result = s.indexOf("x"); System.out.println(result); } } Choose the correct answer: A. -1 B. 0 C. 1 D. 2 E. 3 F. 4 G. This program will not compile H. This program will fail with an exception at runtime

A. -1

You have written the following function to add two numbers: public int add(int a, int b) { return a + b; } Now, you want to add a multi-line-comment saying Add two numbers one one line and Returns an integer on the second line. How can you do this? Choose the correct answer: A. /* Add two numbers Returns an integer */ B. // Add two numbers Returns an integer // C. /// Add two numbers Returns an integer /// D. None of these. It is not possible to add

A. /* Add two numbers Returns an integer */

You run the following program: import java.util.Random; class ExamPrep { public static void main(String[] args){ Random r = new Random(23); int a = r.nextInt(100); int b = r.nextInt(100); int c = r.nextInt(100); System.out.printf("%d,%d,%d%n", a, b, c); } } The output is 27,48,83. What will the output be if you run it again? Choose the correct answer: A. 27,48,83 B. A different set of three random numbers between 0 (inclusive) and 100 (exclusive) C. This code will not compile D. This code will fail with an exception at runtime

A. 27,48,83 The seed, 23 provided to Random's constructor determines the order of pseudorandom values that will be returned by calling nextInt(). If you run the code again using the same seed, you will get the same sequence of numbers.

You are working on a Java project and see the following line of code within a function: final int rate = 5; What does the keyword final do? Choose the correct answer: A. A compile-time error will occur if another line of code attempts to assign a value to the rate variable. B. A compile-time warning will occur if another line of code attempts to assign a value to the rate variable. C. A runtime error will occur if another line of code attempts to assign a value to the rate variable. D. A runtime warning will occur if another line of code attempts to assign a value to the rate variable.

A. A compile-time error will occur if another line of code attempts to assign a value to the rate variable.

Which of these are features of Java? Choose 3: A. Object Oriented. B. Platform Independent C. Context Independent D. Single Threaded E. Multi Threaded

A. B. & E.

You have the following program in a file called hello.java that reads the contents of a file called data.txt and prints the number of lines contained within it. import java.io.IOException; import java.nio.file.*; import java.util.List; class ExamPrep { public static void main(String[] args) { Path path = Path.of("data.txt"); try { System.out.println(countLines(path)); } catch (IOException e) { System.out.println(e.getMessage()); } } static int countLines(Path path) { List<String> lines = Files.readAllLines(path); return lines.size(); } } When you compile this program using javac hello.java, you get the following error: hello.java:10: error: exception IOException is never thrown in body of corresponding try statement } catch (IOException e) { ^ hello.java:16: error: unreported exception IOException; must be caught or declared to be thrown List<String> lines = Files.readAllLines(path); 2 errors You want to catch the error in main() and display the error message. What change do you need to make so that this code will compile? Choose the correct answer: A. Change static int countLines(Path path) { to static int countLines(Path path) throws IOException { B. Change static int countLines(Path path) { to static int countLines(Path path) throws Exception { C. Add a try/catch statement inside countLines and re-throw the exception. D. You cannot catch an exception that occurred within another function. You must move the code out of countLines() and into main().

A. Change static int countLines(Path path) { to static int countLines(Path path) throws IOException {

In Object-Oriented Programming, what is the blueprint, or prototype of from which objects are created? Choose the correct answer: A. Class B. Instance C. Struct D. Type E. Variable

A. Class

An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions. Which of the following statements about Java Exceptions are TRUE? Choose 2 correct answers: A. Exceptions can occur in one function, and caught using a try-catch block in a function that called it or any function higher in the call stack. B. Exceptions must be caught using a try-catch block in the function where they occur. C. Creating an exception object and handing it to the runtime system is called raising an exception D. Creating an exception object and handing it to the runtime system is called throwing an exception

A. Exceptions can occur in one function, and caught using a try-catch block in a function that called it or any function higher in the call stack. D. Creating an exception object and handing it to the runtime system is called throwing an exception

Which of the following statements are TRUE about the java.lang package? Choose the correct answer: A. It provides classes that are fundamental to the design of the Java programming language, like Object, Class, and primitive wrapper classes like Boolean, Integer, and Float. B. It provides classes that enable Java programs to provide localization of text for multi-language support C. It provides classes that allow Java to call functions exported by programs written in other languages and provides the capability to export Java program functions for use by programs written in other programming languages. D. None of these statements are true

A. It provides classes that are fundamental to the design of the Java programming language, like Object, Class, and primitive wrapper classes like Boolean, Integer, and Float.

Java provides looping statements such as for, while, and do-while. Which of the following statements are TRUE about Java's looping statements? Choose all of the correct answers. A. Looping statements provide a way to repeatedly execute the code within the loop as long as a specified condition is true B. Looping statements provide a way to repeatedly execute the code within the loop as long as a specified condition is false C. Java executes each iteration of a loop on a separate thread D. Looping statements improve your code's performance E. The JVM limits the number of iterations to a default of 100 unless you pass the -MaxLoop argument when running your application

A. Looping statements provide a way to repeatedly execute the code within the loop as long as a specified condition is true

What can you add at LINE 6 to print the values of a, b, and c on separate lines, using the newline character that is appropriate for the platform that is running the application? class ExamPrep { public static void main(String[] args){ int a = 1; int b = 2; int c = 3; // LINE 6 System.out.println(result); } } Choose the correct answer: A. String result = String.format("%d%n%d%n%d%n", a, b, c); B. String result = String.format("%d\n%d\n%d\n", a, b, c); C. String result = String.format("%d\r\n%d\r\n%d\r\n", a, b, c); D. String result = String.concat(a, b, c).split();

A. String result = String.format("%d%n%d%n%d%n", a, b, c);

What can you add at LINE 5 to make the program print JavaExam? class ExamPrep { public static void main(String[] args){ String a = "Java"; String b = "Exam"; // LINE 5 System.out.println(result); } } Choose 2 correct answers. Each answer represents a complete solution. A. String result = a + b; B. String result = a & b; C. String result = a.concat(b); D. String result = b.concat(a); E. String result = String.concat(a, b);

A. String result = a + b; C. String result = a.concat(b);

You have a variable named a that is an int array of size 4, defined like this: int a[] = { 99, 98, 97, 96 }; Which line of code will print 96? Choose the correct answer: A. System.out.println(a[3]); B. System.out.println(a[4]); C. System.out.println(a[96]); D. None of these. There is a syntax error in the array declaration.

A. System.out.println(a[3]);

You are working on a project that has an existing function called getNextId() which returns an int. You are given the following code: while (true) { int id = getNextId(); if (id == -1) { break; } if (id % 2 == 0) { continue; } System.out.println(id); } System.out.println("Done"); Which of the following statements are TRUE about this code? Choose the correct answer: A. The code will only print odd numbers B. The code will only print even numbers C. The code will print all numbers except -1 D. If id is an odd number, the execution will exit the loop (or "continue" out of the loop) and print Done. E. If id is an even number, the execution will exit the loop (or "continue" out of the loop) and print Done.

A. The code will only print odd numbers


Related study sets

Bone Tissue and the Skeletal System

View Set

464 prepu ch 3 - Growth and Development of the Newborn and Infant

View Set

Chapter Two - Small Business Entrepreneurs: Characteristics and Competencies

View Set

Health Care Delivery & Evidence-Based Practice

View Set

Meteorology ch 7 atmosphere question bank

View Set

Chapter 48 (Skin Integrity & Wound Care)

View Set