AP JAVA Semester 1

Ace your homework & exams now with Quizwiz!

Given the following declaration, which line of Java code properly casts one type into another without data loss? { int z=5, m=6; } A: double x = (double)(z/m); B: double x=z/m; C: double x = double z/m; D: double x = (double)z/m;

D

Identify which situation could be an example of a while loop A: Taking coins out of a pile one at a time and adding their value to the total until there are no more coins in the pile to add. B: Attending class while school is not over for the day. C: Petting each animal at the pet store one at a time until all the animals have been petted. D: All of the above

D

The three logic operators in Java are: A: &&, !=, = B: &, |, = C: !=, , == D: &&, ||, !

D

This keyword is used to instruct specific code when the input for a switch statement that does not match any of the cases. A: switch B: case C: break D: default E: None of the above

D

What value is assigned to x? int x = 25 - 5 * 4/2 - 10 + 4; A: 8 B: 9 C: 34 D: 7

B

Which two are valid array declarations? A: int array size; B: int[] size; C: int size[]; D: []int size;

B C

What is the output? int[] arr = new int[5]; for(int i=0; i<arr.length; i++){ arr[i] = i; } for(int i=0; i<arr.length; i++){ System.out.print(arr[i]); } A: 012345 B: 12345 C: 01234 D; 123

C

Which is not used to traverse an ArrayList? A: for-each loop B: iterator C: do-while loop D: ListIterator

C

Automatic promotion from smaller data type to a larger data type is not allowed in Java A: True B: False

B

A workspace can have one or more stored projects. A: True B: False

A

An object may interact with another object by invoking methods. A: True B: False

A

It's best-practice to close the Scanner stream when finished. A: True B: False

A

Which is used to terminate a loop? A: break B: switch C: catch D: continue

A

Which two are valid? A: double doubleVar1 = 3.1; double doubleVar2 = 3.1; B: double doubleVar1, doublevar2 = 3.1; C: double doubleVar1, double doubleVar2 = 3.1; double doubleVar1; DoubleVar2 = 3.1

A B

Which two statements compile? A: String name = "Java"; B: String name = 'Java'; C: String name = new String('Java'); D: String name = "J";

A D

Which of the following is the name of a Java primitive data type? A: Object B: int C: Rectangle D: String

B

All of the following are essential to initializing a for loop, except which one? A: Initializing the iterator(i). B: Having a conditional statement. C: Having an if statement. D: Updating the counter.

C

Given the following declaration, which line of Java code properly casts one type into another without data loss? int i = 3, j =4; double y = 2.54; A: double x = i/j; B: double x = (double)(i/j); C: double x = (double)i/j; D: int x = (double)2.54; E: double x = double i/j;

C

Which of the following statements is not a valid array declaration? A: float[] averages; B: int number[]; C: counter int[]; D: double marks[5];

C

Which three are conditional statements? A: for loop B: do while loop C: if statement D: if/else statement E: switch statement

C D E

What is the output of the following segment of code if the command line arguments are "apples oranges pears"? public class Application{ public static void main(String[] args){ System.out.println(args[2]); } } A: This code does not compile B: oranges C: apples D: args E: pears

E

Assigning a value to a variable is called "initialization." A: True B: False

A

Char data types cannot handle multiple characters. A: True B: False

A

How would you use the ternary operator to rewrite this if statement? if(gender == "female") System.out.print("Ms"); else System.out.print("Mr"); A: System.out.print((gender == "female")? "Ms.":""Mr."); B: (gender == "female") ? "Ms." : "Mr."; C: (gender == "female") ? "Mr." : "Ms."; D: System.out.print((gender == "female") ? "Mr." : "Ms.";

A

In Java, char is a primitive data type, while String is an object data type. A: True B: False

A

In object oriented programming, an object comprises of properties and behaviors where properties are represented as fields of the object and behaviors are represented as methods. A: True B: False

A

Once an object is instantiated, how might its fields and methods be accessed in Java? A: Using the dot(.) operator. B: Using the colon(:) operator. C: Using the comma(,) operator D: Using the double-colon(::) operator.

A

System.in readies Scanner to collect input from the console. A: True B: False

A

The Java compiler automatically promotes byte, short, and chars data type values to int data type. A: True B: False

A

The Scanner class considers space as the default delimiter while reading the input. A: True B: False

A

Two variables are required to support a conversion of one unit of measure to another unit of measure. A: True B: False

A

What is the output of the following segment of code? int array[][] = {{6,4,3},{4,3,2}}; for(int = 0; i < 2; i++){ for(int j = 0; j < 3; j++){ System.out.print(2*array[1][1]); }} A: 666666 B: 1286864 C: This code does not compile D: 643432 E: 262423242322

A

What is the output: {public static void main (String args[]) { int j = 7, k = 5, result; result = j - k % 2 * m System.out.println(result);}} A: -9 B: 0 C: 16 D: 2

A

What is the output? public class Person { public static void main(String args[]){ int age = 20; System.out.println("Value of age: " + age); age = 5 + 3; System.out.println("Value of age: " + age); age = age + 1; age++; System.out.println("Value of age: " + age); A: Value of age: 20 \n Value of age: 8 \n Value of age: 10 B: Value of age: 20 \n Value of age: 28 \n Value of age: 38 C: Value of age: 20 \n Value of age: 208 \n Value of age: 20810 D: Value of age: 20 \n Value of age: 8 \n Value of age: 9

A

What is the output? public static void main(String args[]){ String alphaNumeric = "Java World!" + 8; System.out.println(alphaNumeric); } A: Java World!8 B: Java World! + 8 C: Java World! 8 D: Compilation error

A

What is the output? public static void main(String args[]){ String firstString = "Java"; firstString = firstString.concat("World"); System.out.println(firstString); } A: JavaWorld B: World C: Java World D: Java

A

What is the output? public static void main(String args[]){ String greeting = "Java World!"; String w = greeting.substring(7,11); System.out.pringln(w); } A: rld! B: rld C: ld! D: orld!

A

When the result of an expression is assigned to a temporary memory location, what is the size of memory allocated? A: The size of the largest data type used in the expression. B: The size of any data type used in the expression. C: A default size is allocated. D: The size of the smallest data type used in the expression.

A

Which class is used to generate random numbers? A: Random B: Integer C: Double D: Number

A

Which code goes in the try block? A: Any code that is likely to cause an exception. B: Any code that is safe from an exception. C: Any code that can handle an exception. D: Any code that is likely to print the exception details.

A

Which exception is thrown when an application attempts to use null when an object is required? A: NullPointerException B: FileNotFoundException C: ArithmeticException D: ArrayIndexOutOfBoundsException

A

Which is the correct declaration for a char data type? A: char size = 'M'; B: char size = 'Medium'; C: char size = "M"; D: char size = "Medium";

A

Which of the following are the arguments in the following method? Employee emp = new Employee(); emp.calculateSalary(100000, 3.2, 15); A: 100000, 3.2, 15 B: emp C: calculateSalary(100000, 3.2, 15); D: emp.calculateSalary(100000, 3.2, 15);

A

Which statement is not true about do-while loops? A: The number of times a do-while loop is executed is dependent upon the value of the counter variable. B:Statements in the loop are executed once until the condition becomes false. C: Statements in the loop are executed once initially, and then the condition is evaluated. D: Statements in the loop are executed repeatedly until the condition becomes false.

A

You want to compute the sum of all the marks of a given subject. Which approach will you choose? A: Looping B: if/else statement C: switch statement D: if statement

A

Which two are the features of the Math class? A: Common math functions like square root are taken care of in the language. B: The Math methods can be invoked without creating an instance of a Math object. C: You don't have to worry about the data type returned from a Math method. D: Math methods can be invoked with Strings as arguments.

A B

You design a circle class with various fields and methods. Which of the following could be fields in this class? (hint: distinguish between properties and behaviors) A: color B: radius C: calculateCircumference() D: calculateDiameter() E: calculateArea()

A B

Which of the following three statements are true about breakpoints? A: The pause code execution. B: They help with debugging. C: They can be used to check the current state of the program. D: They insert break statements. E: They abruptly end the code execution.

A B C

What are two disadvantages of adding print statements for debugging? A: It's tedious to remove print statements. B: Print statements cannot print the value of an object's field. C: Too many print statements lead to information overload. D: Print statements cannot print the values of variables

A C

What two advantages of adding print statements for debugging? A: You can identify the order in which methods have been called. B: You can identify compilation errors. C: You can identify runtime errors. D: You can identify which methods have been called

A D

Which of the following two operations are appropriate for the main method? A: Creating instances of objects. B: Assigning memory to the variables. C: Calling local variables declared within a class's method. D: Calling an instance object's field and method.

A D

Which of the following two statements are true about variables? A: They allow code to be edited more efficiently B: Variables will be ignored by compiler C: The value assigned to a variable may never change. D: They make code more flexible.

A D

Which two are valid import statements of the Scanner class? A: import java.util.*; B: import java.*; C: import java.util; D: import java.util.Scanner;

A D

Assuming x is an int, which of the following are ways to increment the value of x by 1? A: x = x + 1; B: x = +1; C: x+; D: x++: E: x += 1;

A D E

Which of the following are considered Whitespace? A: Blank lines between code. B: Space in the print statement. C: Space between the [] braces. D: Space between words. E: Indentation before code.

A D E

How could you declare an ArrayList so that it can store true false values? A: ArrayList <boolean> arrList = new Array List<>(); B: ArrayList <Boolean> arrList = newArrayList<>(); C: ArrayList <True,False> arrList = new ArrayList<>(); D: ArrayList <true,false> arrList = newArrayList<>();

B

The following defines a package keyword: A: Precedes the name of the class. B: Defines where this class lives relative to other classes, and provides a level of access control. C: Provides the compiler information that identifies outside classes used within the current class.

B

The following defines an import keyword: A: Defines where this class lives relative to other classes, and provides a level of access control. B: Provides the compiler with information that identifies outside classes used within the current class. C: Precedes the name of the class.

B

What are the possible values of a boolean data type in Java? A: yes/no B: true/false C: 0/1 D: good/bad

B

What is a loop? A: A keyword used to skip over the remaining code. B: A set of logic that is repeatedly executed until a certain condition is met. C: A segment of code that may only ever be executed once per call of the program D: None of the above

B

What is the output? int[] array = {10,20,30}; int b = 0; try{ System.out.println("1"); int c = (array[3] / b); System.out.println("2"); } catch(ArithmeticException ex){ System.out.println("Arithmetic Exception"); } catch(ArrayIndexOutOfBoundsException ex){ System.out.println("Array index out of bounds"); } A: 1 Arithmetic Exception B: 1 Array index out of bounds C: 1 2 D: Array index out of bounds E: 1 2 Array index out of bounds

B

What is the output? public static void main(String args[]){ String greeting = "Java World!"; String w = greeting.replace("a", "A"); System.out.println(w); } A: Java World! B: JAvA World! C: JAva World! D: JavA World!

B

Which is a valid way to parse a String as an int? A: int intVar1 = "100"; B: int intVar1 = Integer.parseInt("100"); C: int intVar1 = Integer.parseInt("One Hundred"); D: int intVar1 = (int)"100";

B

You have a beautiful garden at home. On Sunday, you start budding your rose plant to make a few more samples of rose plants to plant in you garden. Can you categorize how this scenario could be represented by classes and instances? A: Samples are the class and the rose plant is the instances of samples. B: Rose plant is the class and the samples generated from the rose plant are instances of that class. C: Rose plant is the object and samples are not instances of the plant because they have not grown yet. D: Samples of the rose plant are called classes and not the actual rose plant.

B

Which two are not logical operators? A: || B: % C: + D: && E: !

B C

Which two are the correct syntax for adding comments? A: Start with two slashes (//). End with two slashes (//). B: Start with two slashes (//). End when the line ends. C: Start with two slashes and a star (//*). End with a star-slash (*/). D: Start with a slash-star (/*). End with a slash-star (/*). E: Start with a slash-star (/*). End with a star-slash (*/).

B E

How should strings be compared? A: = B: The equals() method C: == D: ~=

C

If an exception is thrown by a method, where can the catch for the exception be? A: The catch must be immediately after the throw. B: There does not need to be a catch in this situation. C: The catch can be in the method that threw the exception or in any other method that called the method that threw the exception. D: The catch must be in the method that threw the exception.

C

In a boolean expression which uses the && operator, what would make this expression evaluate to true? boolean x = (firstCondition && secondCondition); A: If the first condition is false, but the second condition is true. B: IF the first condition is true, but the second condition is false. C: If both the first condition and second condition are true. D: If both the first condition and second condition are false.

C

What is an array? A: An array is an indexed container that holds a set of values of multiple types. B: An array is a way to create multiple copies of a single value. C: An array is an indexed container that holds a set of values of a single type. D: An array is a Java primitive type.

C

What is the content of the array variable table after executing the following code? int[][] table = new int [3][3]; for(int i=0; i<3; i++) for(int j=0; j<3; j++) if(j<i) table[i][j] = 1; A: 001 010 100 B: 111 011 001 C: 100 110 111 D: 100 010 001

C

Which is a valid way of calling the testMethod in the TestClass? public void testMethod(int x, double y){ System.out.println(x/y) } A: testInstance.testMethod(10); B: tesInstance.testMethod(3.5); C: testInstance.testMethod(10,3.5); D: testInstance.testMethod(3.5,10); E: testInstance.testMethod(10,3.5,0);

C

Which method returns the length of a String? A: compareTo() B: findLength() C: length() D: charAt()

C

Which of the following could be a reason to use a switch statement in a Java program? A: Because it terminates the current loop. B: Because it allows the code to run through until a certain conditional statement is true. C: Because it allows the program to run certain segments of code and neglect to run others based on the input given. D: Because it allows the user to enter an input in the console screen and prints out a message that the user input was successfully read in.

C

Which of the following creates a String reference named str and instantiates it? A: String str; B: String s = "str"; C: String str = new String("str"); D: str = "str";

C

Which of the following declares and initializes a one dimensional array that can hold 5 Object reference types? A: Object[] array = new Object[4]; B: String[] array = new String[5]; C: Object[] array = new Object[5]; D: String[] array = String[4];

C

Which of the following scenarios would be ideal for writing a method? A: To group similar data types together. B: When you don't find similar lines of code to describe an object's behavior. C: When you don't want to repeat similar lines of code to describe an object's behavior. D: For every five to six lines of code.

C

Which statement is true about packages? A: A package makes it difficult to locate the related classes. B: Packages of the Java class library do not contain related classes. C: A package contains a group of related classes. D: A package doesn't contain a group of related classes.

C

You need to generate random integer values between 0 and 80 (inclusive). Which statement should you use? A: nextInt(80); B: nextInt(); C: nextInt(81); D: nextInt(0-79);

C

Which are used in boolean expressions? A: Errors B: Loops C: Operators D: Variables

C D

Which two statements are correct about the usage of an underscore? A: Underscores change the value of the number. B: Underscores help the compiler interpret large numbers. C: Underscores help make large numbers more readable. D: Underscores do not affect the value of the variable.

C D

Which two statements are true about the default statement? A: A default statement is executed by default when the program is executed. B: A default statement is required in every switch statement. C: When the input does not match any of the cases, the default statement is executed. D: A default statement is optional in a switch statement.

C D

The JFrame and JOptionPane classes are in the javax.swing package. Which three will import those classes? A: import javax.swing; B: import javax.swing.J* C: import javax.swing.*; D: import javx.swing.JOptionPane; E: import javax.swing.JFrame;

C D E

Given the expression:String message = "Hello World"; Which is the String Literal? A: String message = "Hello World"; B: String message C: message D: Hello World

D

In the given syntax of for loop, which part represents the header section? for (initialization; condition; update){ // Code statement(s) } A: for (initialization; condition;update){} B: for (initialization;condition;update){Code statement(s)} C: Code statement(s) D: for (initialization;condition;update)

D

Object instantiation is done using what keyword? A: void B: instance C: System D: new

D

What is the output of the following segment of code? int array[][] = {{2,2,2},{2,2,0}}; for(int i = 0; i < 2; i++){ for(int j = 0; j < 3; j++){ if(array[i][j] == 0 && j == 2 System.out.print(array[i][j]); }} A: 220 B: 222220 C: 2 D: 0 E: This code does not compile

D

What is the output: public class Welcome { public static void main(String args[]) { System.out.println("This is my first program"); Int a = 2; System.out.println("a is" + a); }} A: This is my first program B: This is my first program \n a is + a C: a=2 D: This is my first program \n a is 2

D

What is the output? int[] arr = new int[1]; arr[0] = 10; System.out.println(arr[0]); A: ArrayIndexOutOfBoundsException B: 1 C: 0 D: 10

D

What is the output? public static void main(String args[]){ String str = "Hello"; str = "World"; System.out.println(str); }} A: Hello World B: Hello, World C: Hello D: World

D

What is the output? public static void main(String args[]){ int x = 100; int y = x; y ++; System.out.println("Value of x is " + x); System.out.println("Value of y is " + y); A: Value of x is 0 \n value of y is 1 B: Value of x is 100 \n value of y is 1 C: Value of x is 100 \n value of y is 100 D: Value of x is 100 \n value of y is 101

D

What will array arr contain after the following code segment has been executed? int[] arr = {5, 4, 2, 1, 0}; for(int i=1; i<arr.length; i++) { arr[i-1] += arr[i]; } A: 9, 6, 1, 3, 0 B: 10, 6, 3, 1, 0 C: 7, 3, 2, 1, 0 D: 9, 6, 3, 1, 0 E: None of the above

D

When is an update expression in a for loop executed? A: Before the first iteration through the loop. B: Before each iteration through the loop. C: After two iterations through the loop. D: After each iteration through the loop.

D

Which is valid syntax to declare and initialize a String variable? A: String "x" = Java; B: String "x" = "Java"; C: String x = Java; D: String x = "Java";

D

Which of the following data types is the largest? A: byte B: Short C: int D: long

D

Which of the following wild card characters is used to import all the classes in a particular package? A: ! B: ; C: ~ D: *

D

Which statement is false about infinite loop? A: An infinite loop is a code which will execute until the user interrupts the program. B: The body of a while loop eventually must make the condition false to avoid infinite loop. C: An infinite loop is generally caused by a programming mistake. D: An infinite loop is commonly the result of a syntax error.

D

You need to calculate the squares of numbers from 1 to 5. Which of the items should be present in your looping statement? A: Initialization Expression, Update Expression B: Initialization Expression, Condition Expression C: Condition Expression, Update Expression. D: Initialization Expression, Condition Expression, Update Expression.

D

Identify the names of the two variables used in the given code. public class Variables { public static void main(String args[]) { String strVal = "Hello"; int intVal = 0; System.out.println("Integer: " + intVal); }} A: Hello B: String C: int D: strVal E: intVal

D E

How many bits are in a byte? A: 2 B: 4 C: 6 D: 7 E: 8

E


Related study sets

Signs and Symptoms for Microbiology Final

View Set

ENVS 1126 practice exam 1, 2016 Fall ENVS 1126 for Edward Laws Test 2

View Set

PNU 119 PrepU Chapter 9: Teaching and Counseling

View Set

ECE Fund of Cybersecurity and Info Security: Ch 12 and 15

View Set