Computer Science Exam #1

Ace your homework & exams now with Quizwiz!

The toUpperCase method of the String class has how many arguments? a) 0 b) 1 c) 2 d) 3

a) 0

In Java, every statement must end with this symbol. a) ; (semicolon) b) . (period) c) : (colon) d) ) (closing parenthesis)

a) ; (semicolon)

What is a parameter variable? a) A variable that is declared in the header of a method. b) variable that is declared in the header of a class. c) A variable that is declared in the body of the class. d) A variable that is declared in the body of a method.

a) A variable that is declared in the header of a method.

Which of the following is true? a) Accessor methods always have a return statement b) Mutator methods never have a return type of void c) Accessor methods always have a return type of void d) Mutator methods always have a return statement

a) Accessor methods always have a return statement

What does this sequence of statements print? Rectangle box = new Rectangle(5, 10, 20, 30); System.out.print("Before: " + box.getX()); box.translate(-5, -10); System.out.println(" After: " + box.getX()); a) Before: 5 After: 0 b) Before: 5 After: 30 c) Before: 5 After: 10 d) Before: 10 After: 0 e) Before: 10 After: 30

a) Before: 5 After: 0

What is wrong with the following code snippet? public class Area { public static void main(String[] args) { int width = 10; height = 20.00; System.out.println("area = " + (width * height)); } } a) The code snippet uses an undeclared variable. b) The code snippet uses an uninitialized variable. c) The code snippet attempts to add a number to a string variable. d) The code snippet attempts to assign a decimal value to an integer variable.

a) The code snippet uses an undeclared variable.

What is wrong with this Letter class shown below? Letter Class { private String recipient; private String sender; private String text; public Letter(String aRecipient, String aSender) { recipient = "John"; sender = aSender; text = ""; } public void setText(String aText) { text = aText; } public void display() { System.out.println("Dear "); System.out.println(recipient); System.out.println(":"); System.out.println(); System.out.println(text); System.out.println(); System.out.println("Sincerely "); System.out.println(); System.out.println(sender); } } a) The letters always go to John. b) The letters always come from John. c) The letters always come from Yoko. d) The text is always empty.

a) The letters always go to John.

Assuming the following Java statement: double gpa = 3.2; What does the variable gpa store? a) The numeric value 3.2. b) The numeric value 3. c) A reference to the memory location where the value 3.2 is stored. d) A reference to the double primitive type. e) An object representing the number 3.2.

a) The numeric value 3.2.

In Java, objects within the same class share common _______. a) behavior b) data c) instructions d) comments

a) behavior

Which of the following is a valid variable name and conforms to Java naming conventions? a) count1 b) 1count c) cat-count d) number of cats e) NumberOfCats

a) count1

Which of the following is a valid variable declaration? Assume that length is a variable of type int and milesPerGallon a variable of type double. a) int volume = length * length * length; b) int gallonsConsumed = milesDriven / milesPerGallon; c) double greeting = "HelloHello"; d) String greeting = 2 * "Hello";

a) int volume = length * length * length;

Which of the following is a mutator method? a) translate (Rectangle class) b) toUpperCase (String class) c) getX (Rectangle class) d) length (String class) e) None are mutator methods

a) translate (Rectangle class)

What does the following statement print? System.out.println(new Rectangle().getWidth()); a) -5 b) 0 c) 5 d) 10 e) 15

b) 0

Consider the following method comment and method header: /** Counts the number of characters in a String object. __________ the number of characters in the String */ public int length() { . . . } What should be in the highlighted section? a) return b) @return c) &gets d) length e) @param

b) @return

What is the name of the constructor for a class named BankAccount? a) getBankAccount b) BankAccount c) BankAccountConstructor d) It depends on what the programmer picks as the constructor name e) BankAccountClass

b) BankAccount

What will get displayed in the terminal window after the following statements run? String myName = "Dr. Fryling"; myNameUpperCase = myName.toUpperCase();; System.out.print(myNameUpperCase); a) Nothing will display. These statements have syntax errors so the program will not compile. b) Dr. Fryling c) DR. FRYLING d) 10 e) 11

b) Dr. Fryling

int 12 = 12; is a valid expression in the Java language? a) True b) False

b) False

What is the output of the following code snippet? String str1; str1 = "SPRING BREAK"; String str2 = str1.substring(3, 8); System.out.println(str2); a) RING BR b) ING B c) ING BR d) RING B

b) ING B

Which one of the following statements displays the output as 54321.00? a) System.out.printf(",8.2f", 54321.0); b) System.out.printf("%8.2f", 54321.0); c) System.out.printf("%8f", 54321.0); d) System.out.printf("%8.00f", 54321.0);

b) System.out.printf("%8.2f", 54321.0);

Which statement is true about the following constructor of the BankAccount class? public BankAccount(double balance) { this.balance = balance; } a) b) The code sets the instance variable balance to the parameter variable balance. c) The code has a syntax error. d) The code has a logic error.

b) The code sets the instance variable balance to the parameter variable balance.

Is the getX method of the Rectangle class a mutator or accessor method? a) mutator b) accessor

b) accessor

What does an object store its data in? a) object variables b) instance variables c) access specifiers d) None of the above e) files

b) instance variables

What are the values of num1 and num2 after this snippet executes? double num1 = 4.20; double num2 = num1 * 10 + 5.0; a) num1 = 4.20 and num2 = 42.0 b) num1 = 4.20 and num2 = 47.0 c) num1 = 42.0 and num2 = 42.0 d) num1 = 42.0 and num2 = 47.0

b) num1 = 4.20 and num2 = 47.0

Which of the following is a correct declaration of a constant for use inside a class? a) final double CM_PER_INCH = 2.54; b) public static final double CM_PER_INCH = 2.54; c) public final double CM_PER_INCH = 2.54; d) public static final double cmPerInch = 2.54;

b) public static final double CM_PER_INCH = 2.54;

The _____________ statement is a special statement that terminates the method call and returns a result to the method's caller. a) getValue b) return c) terminate d) returnValue

b) return

What term is used to refer to a sequence of characters enclosed in quotation marks? a) Object b) string c) variable d) comment

b) string

How should the following test program be completed (i.e. what should be in the highlighted section of the println method call)? import java.awt.Rectangle; public class MoveTester { public static void main(String[] args) { Rectangle box = new Rectangle(5, 10, 20, 30); // Move the rectangle box.translate(15, 0); // Print information about the moved rectangle System.out.print("x: "); System.out.println(box.getX()); System.out.println(___________); } } a) box.getY() b) 20 c) "Expected: 20" d) The last line should be removed.

c) "Expected: 20"

What is the output of the following code: Rectangle r1 = new Rectangle(5,10,20,30); Rectangle r2 = r1; r1.translate(10,10); System.out.println(r2.getX()); a) 5 b) 10 c) 15 d) 20 e) 25

c) 15

Assuming that the user enters 45 and 62 as inputs for n1 and n2, respectively, what is the output of the following code snippet? public static void main(String[] args) { System.out.print("Enter a number: "); Scanner in = new Scanner(System.in); String n1 = in.next(); System.out.print("Enter another number: "); String n2 = in.next(); String result = n1 + n2; System.out.print(result); } a) 46 b) 107 c) 4562 d) 4662

c) 4562

What would the following code snippet print? int value = 25; value = value * 2; value--; System.out.println(value); a) 25 b) 26 c) 49 d) 50

c) 49

Assume the class Circle has an accessor called getRadius and a mutator called setRadius. What is the output of the following code: Circle c1 = new Circle(3); Circle c2 = new Circle(4); c2.setRadius(6); c1 = c2; c1.setRadius(5); System.out.println(c2.getRadius()); a) 3 b) 4 c) 5 d) 6

c) 5

Assuming the following Java statement: Rectangle rec = new Rectangle(5, 10, 25, 25); What does the variable rec store? a) The constructed object itself. b) A reference to the Rectangle class. c) A reference to the memory location of the constructed object named rec. d) The four numeric values of 5, 10, 25, & 25.

c) A reference to the memory location of the constructed object named rec.

What is a local variable? a) A variable that is declared in the header of a class. b) A variable that is declared in the body of the class. c) A variable that is declared in the body of a method. d) A variable that is declared in the header of a method.

c) A variable that is declared in the body of a method.

What is the JVM? a) A vital machine that never fails to run compiled Java code. b) A vital machine that compiles Java code into machine instructions. c) A virtual machine that runs compiled Java code on any CPU. d) A virtual machine that compiles Java code into machine instructions.

c) A virtual machine that runs compiled Java code on any CPU.

A(n) _________________ is a program that translates high-level instructions into more detailed instructions that can be executed by the CPU. a) Debugger b) Applet c) Compiler d) Linker

c) Compiler

What will get displayed in the terminal window after the following statements run? String myName = "Dr. Fryling"; myName = myName.toUpperCase(); System.out.print(myName.length()); a) Nothing will display. These statements have syntax errors so the program will not compile. b) Dr. Meg c) DR. MEG d) 10 e) 11

c) DR. MEG

____________________ allows a programmer to use a class without having to know the details of its implementation. a) Abstraction b) Instantiation c) Encapsulation d) Interface

c) Encapsulation

A Java source file that contains the public class Example1 must be named ____________. a) example1.java b) 1Example.java c) Example1.java d) Example.java

c) Example1.java

What will the set of statements print? a) My lucky number is 3 + 4 + 5 b) My lucky number is 12 c) My lucky number is12 d) None of the above

c) My lucky number is12

Which of the following represents a correct way to declare and initialize a variable named myProfessor? a) myProfessor = "Dr. Fryling"; b) int myProfessor = "Dr. Fryling"; c) String myProfessor = "Dr. Fryling"; d) String myProfessor = Dr. Fryling; e) double String myProfessor = Dr. Fryling;

c) String myProfessor = "Dr. Fryling";

Assuming that the user inputs a value of 25000 for the pay and 10 for the bonus rate in the following code snippet, what is the output? public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter the pay: "); double pay = in.nextDouble(); System.out.print("Enter the bonus rate: "); double bonus = in.nextDouble(); System.out.println("The new pay is " + (pay + pay * (bonus / 100.0))); } a) The new pay is 25000 b) The new pay is 25100 c) The new pay is 27500 d) The new pay is 30000

c) The new pay is 27500

Which of the following is true about constructors? a) All of the above statements are true b) Their return type is always void. c) Their name is always the same as the class name. d) You can only have one constructor for each class.

c) Their name is always the same as the class name.

Which one of the following statements can be used to get the fifth character (i.e. the character 'd') from a string str? String str = "Sheldon"; a) char c = str[4]; b) char c = str[5]; c) char c = str.charAt(4); d) char c = str.charAt(5);

c) char c = str.charAt(4);

What contains the instructions to initialize the instance variables of an object? a) mutator method b) accessor method c) constructor d) initializer e) main method

c) constructor

Assuming that variables dollars and cost have been declared, which of the following assignment statements is INVALID? a) cost = dollars + 100; b) cost = cost + 50; c) dollars + 100 = cost; d) dollars = cost;

c) dollars + 100 = cost;

Which of the following is a valid variable declaration? Assume the following variables have already been declared/initialized. double width = 2.0; double length = 5.0; a) int area = length * width; b) int area = 3 * width; c) double area = 3 * width; d) area = length * width; e) String area = length * width;

c) double area = 3 * width;

To use the Random class in your program you need to ________ its package it. a) overload b) attach c) import d) export e) link

c) import

A method is a sequence of ___ that accesses the data of an object. a) data b) objects c) instructions d) streams

c) instructions

A __________ contains sequences of instructions to perform a particular task. a) parameter b) label c) method d) variable e) All of the above

c) method

Imagine a getArea method that calculates and returns the area of a Square. Which of the following corresponds to the getArea method header for a Square class assuming an int value for a side length (select all that can apply)? a) public String getArea() b) public void getArea() c) public double getArea() d) public int getArea()

c) public double getArea() d) public int getArea()

What is the output of the following code: int num1 = 6; int num2 = num1; num2 = num2 + 10; System.out.println(num2); a) 6 b) 10 c) 4 d) 16 e) 12

d) 16

Consider the following invocation of the deposit method: mySavings.deposit(250); What is the explicit parameter? a) mySavings b) deposit c) There is no explicit parameter. d) 250

d) 250

Which of the following statements correctly calls a constructor with no construction arguments? a) A call to a constructor must have construction arguments. b) Rectangle rec = Rectangle(); c) Rectangle rec = new Rectangle(5, 2, 15, 30); d) Rectangle rec = new Rectangle(); e) Rectangle = new Rectangle();

d) Rectangle rec = new Rectangle();

Which of the given System.out.print statements generates the following output? ABCDE"\ a) System.out.println("ABCDE"\); b) System.out.println("ABCDE"\"); c) System.out.println("ABCDE\"\"); d) System.out.println("ABCDE\"\\");

d) System.out.println("ABCDE\"\\");

When are local variables initialized? a) You must initialize local variables in the constructor. b) Local variables are initialized with a default value before a constructor is invoked. c) Local variables are initialized when the method is called. d) You must initialize local variables in a method body.

d) You must initialize local variables in a method body.

What will be the value inside the variables a and b after the given set of assignments? int a = 20; int b = 10; a = (a + b) / 2; b = a; a++; a) a = 15, b = 15 b) a = 16, b = 16 c) a = 15, b = 16 d) a = 16, b = 15

d) a = 16, b = 15

A programmer wishes to insert a comment following an assignment statement. Which of the following will generate an error? a) cost = 25.0; // initial cost of a product b) cost = 25.0; /* initial cost of a product */ c) cost = 25.0; // initial cost of a product */ d) cost = 25.0; /* initial cost of a product //

d) cost = 25.0; /* initial cost of a product //

Which of the following represents a correct way to declare and initialize a variable named myGPA? a) myGPA = 1; b) double myGPA = 1; c) double myGPA = "one"; d) double myGPA = 1.0;

d) double myGPA = 1.0;

A(n) _________________ is the software that allows a programmer to write, edit, and test programs. a) editor b) browser c) compiler d) integrated development environment e) All of the above

d) integrated development environment

Which of the following is a valid variable name? a) miles per gallon b) miles/gallon c) 5milesPerGallon d) milesPerGallon

d) milesPerGallon

You should declare all instance variables as ______________ . a) protected b) public c) None of the above d) private e) class

d) private

Assuming the programmer wishes to output the phrase "Welcome!", which of the following is true about the following Java statement. System.out.printline("Wlcome!"); a) There are no errors. b) There is a compile-time error. c) There is a run-time error. d) There is a compile-time error and a run-time error.

d) there is a compile-time error and a run-time error

Consider the constructor of the BankAccount class that has a parameter for the initial balance. Which line of code is equivalent to this constructor body? balance = initialBalance; a) balance = this.initialBalance; b) this.balance = this.initialBalance; c) this.initialBalance = balance; d) this.balance = initialBalance

d) this.balance = initialBalance

What is the value of Math.pow(3, 2)? a) 5 b) 6 c) 7 d) 8 e) 9

e) 9

The java.lang package is automatically imported in our Java programs. Which class is part of the java.lang package? a) Rectangle b) PrintStream c) Random d) Rectangle e) String

e) String

What is the return type of the toUpperCase method of the String class? a) double b) Need more information to answer this question. c) void d) int e) String

e) String

What is the output of the following code? double unitPrice = 5.5; int quantity = 2; System.out.print("Total price"); System.out.println(unitPrice * quantity); a) Total price: 11.0 b) Total price11 c) Total price 11.0 d) Total price 11 e) Total price11.0

e) Total price11.0

Which of the following is true about ENIAC? a) First electronic general-purpose computer b) Acronym for Electronic Numerical Integrator And Computer c) Was initially designed to calculate artillery firing tables for the US Army's Ballistic Research Laboratory d) Capable of being reprogrammed to solve a full range of computing problems e) All of the above

e) all of the above

What will the following code print? String sentence = "Mary had a little lamb."; StringTokenizer t1 = new StringTokenizer(sentence); StringTokenizer t2 = new StringTokenizer(sentence); t1.nextToken(); t1.nextToken(); t2.nextToken(); System.out.println(t2.nextToken()); a) Mary b) little c) a d) lamb e) had

e) had

Which variable declaration and initialization is NOT valid? a) double num = 3 * 4; b) int num = 3 * 4; c) String num = "3 * 4"; d) double num = 3.0 * 4; e) int num = 3.0 * 4;

e) int num = 3.0 * 4;

Which method would you use to obtain the string "Hello, World!" from the string " Hello, World! "? a) replace b) length c) toUpperCase d) remove e) trim

e) trim

How many arguments does the Rectangle getY method expect?

0

What is the value of the area variable after all the code below is executed? int width = 5; int height = 10; width = width + 5; int area = width * height;

100

What is the data type of each of the following numbers? 10 -3 3.14 300

int int double int

System.out is an object of the ___ class.

PrintStream

What data type should be used in the variable declaration and initialization below? _____ age = "3";

String


Related study sets

EMT Chapter 31 - Orthopaedic Injuries, EMT - Chapter 31: Orthopaedic Injuries

View Set

World Civic Chapter 28: The Building of Global Empires

View Set

AMSCO. Chapter 13 : Union in Peril | Multiple Choice Questions

View Set

Chapter 2: Accessing the Command Line

View Set

Florida Laws and Rules Pertinent to Insurance CH. 8

View Set