Java 1

Ace your homework & exams now with Quizwiz!

What company developed Java?

Sun Microsystems

The following loop is syntactically correct. for (; ; );

True

What is the exact output of the following code segment? if ( 23 <= 5) if (4 >= 2) System.out.print ("middle "); else System.out.print ("not "); System.out.print ("end"); a) end b) not end c) middle end d) middle not end e) nothing

a

What is invoked to create an object?

a constructor

Which of the following is a character literal? a. private byte num; b. 'y' c. The Character class d. import java.io.*; e. char a;

b

Which of the following lines of code contains a reference variable? a. public static final int PLAYER1 = 0; b. Scanner scan = new Scanner(System.in); c. private int score; d. public void setVolume(double volumeIn) e. converted = (double)num;

b

Which of the following consecutively executed lines of code performs conversion via promotion? a. int number = 6; b. double value = 2.3, actual = number; c. System.out.println((double)number / value); d. actual = number / value; e. value = value / 2.0;

d

Which of the following is a valid constructor header for the Product class? a. public int Product(String name, double price) b. public void Product(String name, double price) c. public Construct(String name) d. public Product() e. Both a and b are valid

d

Which of the following lines would print the String referenced by the variable eventName in double quotes? a. System.out.println("\"eventName\""); b. System.out.println(eventName); c. System.out.println("eventName"); d. System.out.println("\""+eventName+"\""); e. System.out.println("+eventName+");

d

What is the natural order of the int values in the array below? int[] numbers = { 4, 3, 1, 2 };

1, 2, 3, 4

How many bits does a long have?

64

How many bits does a byte have?

8

When an object is created, space is reserved in memory for that object's ________________.

instance data

If a program compiles fine, but it produces incorrect result, the the program suffers a _______________ error.

logical

6. What is the exact output of the following code segment? if ( 13 <= 5) if (4 <= 2) d) happy end d) nothing e) nothing e) nothing } a) red System.out.print ("red "); else { System.out.print ("blue "); System.out.print ("end"); b) blue end c) red end d) red blue end e) nothing

nothing

What is the exact output of the following code segment? if(13<=5) { if (4 <= 2) System.out.print ("red "); else System.out.print ("blue "); System.out.print ("end"); } a) end b) blue end c) red end d) red blue end d) red blue end e) nothing

nothing

What is the relationship between a class and an object?

objects are instances of classes

Parameters to methods always appear inside of?

parentheses ( )

A private method is referred to as a(n) ________ method, while a public method is referred to as a(n) ________ method. a. static, class b. class, static c. support, service d. static, service e. service, static

C

Analyze the following code. public class Test { public static void main(String[] args) { int[] x = new int[3]; System.out.println("x[0] is " + x[0]); } } a) The program has a compiler error because the size of the array wasn't specified when declaring the array. b) The program has a runtime error because the array elements are not initialized. c) The program runs fine and displays x[0] is 0. d) The program has a runtime error because the array element x[0] is not defined. e) none of the above

C

Fill in the blank in the Javadoc comment below: /** * Adds interest to the account balance. * * @________ The interest. */ public void addInterest(double rate) a. param b. return c. param rate d. return rate e. param args

C

To set a FlowLayout in panel jp, you can use the method __________. a) jp.setLayout(new FlowLayout(FlowLayout.center)); b) jp.setLayout(new FlowLayout(FlowLayout.CENTER)); c) jp.setLayout(new FlowLayout()); d) jp.setLayout(FlowLayout()); e) a or b

C

What is the printout of the following switch statement? char ch = 'b'; switch (ch) { case 'a': System.out.print(ch); case 'b': System.out.print(ch); case 'c': System.out.print(ch); case 'd': System.out.print(ch); } a) abcd b) bcd c) bbb d) bb e) b

C

Which of the following lines contains a narrowing conversion? a. double value1 = Double.parseDouble(str); b. int value = "Exam 1!".length(); c. double value3 = (int) 3.5; d. int value4 = 4;

C

Aggregation is a) described as a has-a relationship b) using an object reference as a local variable c) a form of software documentation d) all of the above e) none of the above

A

The default layout out of a contentPane in a JFrame is __________. a) FlowLayout b) GridLayout c) BorderLayout d) GridBagLayout e) TabbedLayout

A

Java arrays can store primitive types and Strings, but cannot store any other type of Object other than Strings. a) True b) False

False

If you define a Java class as: public class Fun { } What should the file name be saved as?

Fun.java

What is the exact output of the following code segment? n1 = 3; n2 = 8; if (n1 <= n2) System.out.print(n1 + " "); System.out.print(n2 + " "); System.out.println ("end"); a) 3 8 end b) 8 end c) 3 8 end d) 8 end e) nothing

a

Which of the following lines directly invokes a constructor? a. String output = new String("Red Sox"); b. public static void main(String[] args) c. module.construct(); d. public class PaymentMethod e. None of the above

a

Which of the following provides the object equivalent of a primitive type? a. Wrapper class b. System class c. The Scanner class d. Math class e. There is no object equivalent of a primitive type

a

Below four variables are declared, of which, three are initialized in the declaration. Which of the variables cannot be set to a new value in a subsequent assignment statement? a. private double length; b. public static final double CEILING = 65; c. String bestTeam = "Patriots"; d. private boolean isIt = true; e. All of the above variables length, CEILING, bestTeam, and isIt can be reassigned values.

b

In which of the following lines is a static method invoked? a. int letters = strInput.length(); b. double x = Double.parseDouble("2.3"); c. cc.setHeight(6); d. public static final int ACE = 1; e. All of the above invoke static methods.

b

Whatistheexactoutputofthefollowingcodesegment? d1 = 13.4; d2 = 9.801; if (d1 == d2) System.out.print ("equal "); else if (d1 > d2) System.out.print ("greater "); else { System.out.print ("less "); System.out.print ("end"); } a) end b) greater c ) greater end d) greater less end e) nothing

b

Which of the following statements is valid? a) int i = new int(30); b) int[] i = {3, 4, 3, 2}; c) double d[30] = new double[]; d) char[] c = new char(); e) char[] c = new char[4]{'a', 'b', 'c', 'd'};

b

What is printed to standard output after the following line of code is executed? System.out.println("The answer is: " + 24 + 25); a. The answer is: 49 b. The answer is: 50 c. The answer is: 2425 d. The answer is: +49 e. The answer is: +50

c

What is the value of guests after the following code is executed? int guests = 0; guests++; a. -1 b. 0 c. 1 d. 2 e. 3

c

Which of the following lines of code invokes a method? a. private double yCoord; b. public static void main(String[] args) c. System.out.println("Invalid input"); d. public class PaymentMethod e. banddonly

c

Why should we use inheritance?

to avoid redundancy, code reuse, testing, maintainability

Suppose a and b are objects of a class that has implemented the comparable interface, and a < b. If a.compareTo(b) is called what is returned?

true

The words ______________ are reserved in Java as Boolean literals.

true, false

int num = 6; while (num < 16) { 711 ́switch (num % 4) { 5 ÷11 ́case 0: System.out.print ("multiple "); 11⁄2 3⁄411 break; 5 ÷11 case 1: System.out.print ("one "); 11⁄2 3⁄411 break; 5 ÷11 ́case 2: System.out.print ("two "); § 5 ÷11Ãcase 3: System.out.print ("three "); 11⁄2 3⁄411 break; } num += 3; }

two three one multiple three

If a method does not have a return statement, then what return type does it have to have?

void

class Short2 { public static void main(String[] args) { int x = 1; int y = x++ + x; System.out.println("y is " + y); y = x + ++x; System.out.println("y is " + y); x = 2; y = 3; double z = Math.pow (y, x); System.out.println ("Square Root of " + z + " = " +Math.sqrt (z)); } }

y is 3 y is 5 Square Root of 9.0 = 3.0

Typing a { when you should have typed a ( is a ______________ error.

compile-time

What is the exact output of the following code segment? if ( 3 <= 15) if (4 >= 2) System.out.print ("happy "); else System.out.print ("sad "); System.out.println ("end"); a) happy b) sad c) sad end d) happy end d) nothing

d

What is the exact output of the following code segment? n1 = 16; d1 = 3.2; n2 = 0; while (d1 < n1) { n2 = n2 + 1; d1 = d1 + 5; } System.out.println (n2 + " " + d1); a) 0 3.2 b) 1 13.2 c)2 18.2 d) 3 18.2 e)nothing

d

Multiplying two numbers when you meant to add them is a _______________ error.

logical

Well encapsulated classes have _______________ variables and ________________ methods.

private, public

When applied to an instance variable, which reserved word (modifier) allows only subclasses and classes in the same package direct access to the instance variable?

protected

Suppose you wish to provide an accessor method for a boolean variable called 'finished'. What method header should be used?

public boolean.getFineshed();

What words are required at the start of every "main" method definition?

public static void

Assuming a s = "ABCABC", what method returns a new string "aBCaBC"?

s.replace('A', 'a')

What is y after the following statement is executed? int x = 0; int y = (x > 0) ? 10 : -10;

-10

What are the four ways to write Java comments?

//comment /**comment*/ /*comment*/ //**comment**\\

What is the exact output of the following code segment? n2 = 0; for (n1 = 24; n1 <= 30; n1 = n1 + 2) n2 = n2 + n1; System.out.println (n2);

108

What is the exact output of the following code segment? char ch; String s1 = new String("this is it"); n1 = 0; n2 = 0; do { if (s1.charAt(n1) == 'i') n2 = n2 + 2; else n2++; n1 = n1 + 1; } while (n1 < s1.length()); System.out.println (n2);

13

How many bits does short have?

16

What is the output of the following code? class Test29{ public static void main(String[] args) { int x = 3; int y = 0; switch (x + 3) { case 6: y = 0; case 7: y = 1; default: y += 1; } System.out.print (y); } } }

2

How many unique items can be represented by 10 bits?

2^10

How many unique items can be represented by one short?

2^16

How many unique items can be represented by one int?

2^32

How many unique items can be represented by one long?

2^64

How many bits does an int have?

32

import java.text.DecimalFormat; import java.util.Random; class Short3{ public static void main(String[] args) { DecimalFormat df = new DecimalFormat("0.##"); double val = 4.12567; System.out.println(df.format(val)); Random rand = new Random(); System.out.println (rand.nextInt (1)); } }

4.13 0

What is the exact output of the following code segment? n1 = 3; while (n1 < 8) { if (n1 % 2 == 0) System.out.print (n1 + " "); n1 = n1 + 3; if (n1 % 3 == 0) System.out.print (n1 + " "); n2 = n1; while (n2 < 6) { System.out.print (n2); } n2 = n2 + 1; }

6 6 9

What does the first System.out.println in the main method print? ÕÖ×public class Foo { § í static int i = 0; § í static int j = 0; § § Þßàpublic static void main(String[] args) { § ̈1í int i = 2; § ̈11 ́{ § § ̈1í int j = 3; § § ̈11 System.out.print("i + j = " + (i + j)); § § ©} § § § ̈11 System.out.println("; j = " + j); § ©} ©} a) i+j=5;j=0 b) i+j=5;j=3 c) i+j=0;j=0 d) i+j=0;j=3 e) compiler error

A

What is the output of the following code? public class Test17 { public static void main(String[] args) { int[] x = {1, 2, 3, 4}; int[] y = x; x = new int[2]; for (int i = 0; i < y.length; i++) System.out.print(y[i] + " "); } } a) 1 2 3 4 b) 0 0 c) 0 0 3 4 d) 0 0 0 0 e) none of the above

A

What is the representation of the third element in an array called a? a) a[2] b) a(2) c) a[3] d) a(3) e) none of the above

A

Assume double[][] x = new double[4][5], what are x.length and x[2].length? a) 4 and 4 b) 4 and 5 c) 5 and 4 d) 5 and 5 e) none of the above

B

To declare a constant MAX_LENGTH as a static variable with value 99.98, you write a) final static MAX_LENGTH = 99.98; b) final static double MAX_LENGTH = 99.98; c) static double MAX_LENGTH = 99.98; d) final double MAX_LENGTH = 99.98; e) final double static MAX_LENGTH = 99.98;

B

What is the output of the following code? public class Test23 { public static void main(String[] args) { int n = 2; xMethod(n); System.out.println("n is " + n); } void xMethod(int n) { n++; } } a) The code has a compiler error because xMethod does not return a value. b) The code has a compiler error because xMethod is not declared static. c) The code prints n is 1. d) The code prints n is 2. e) The code prints n is 3.

B

What is wrong in the following code? class TempClass { int i; public void TempClass(int j) { int i = j; } } public class C { public static void main(String[] args) { TempClass temp = new TempClass(2); } } a) The program has a compilation error because TempClass does not have a default constructor. b) The program has a compilation error because TempClass does not have a constructor with an int argument. c) The program compiles fine, but it does not run because class C is public. d) a and b. e) None of the above.

B

When a class implements an interface, what must it do? a) it must redefine each constant from the interface. b) it must declare and provide a method body for each method in the interface. c) it must declare a variable for each constant in the interface. d) it must include a private method for each method in the interface. e) none of the above

B

Which of the following (if any) is not a valid variable name? a. old b. new c. total$ d. an_input e. All of the above are valid variable names.

B

Which of the following is executed first when you run your Java application? a. The constructor of your class b. The main method of your class c. The toString method of your class d. All static methods of a class e. None of the above

B

Which of the following represents the Java interpreter on your computer and is responsible for running your Java program? a. jGRASP .exe b. java.exe c. javac.exe d. run.exe e. Any IDE can act as a Java interpreter.

B

Which of the following best describes the variable num? Integer num = new Integer(10); a. a double type variable b. a reference type variable c. a primitive type variable d. a static variable e. a constant field

B. a reference type variable

If you wanted an alternating ordering for sorting items which interface should you implement?

Comparator

Java is both interpreted and ______________?

Compiled

In order to implement Comparable in a class, what method(s) must be defined in that class? a) equals b) compares c) both lessThan and greaterThan d) compareTo e) both compares and equals

D

Suppose that your program produces an incorrect answer as a result of integer division. What type of error is produced? a. a style error b. a compile-time error c. a run-time error d. a logical error e. All of the above are valid answers.

D

Variables that are shared by every instances of a class are ________________ variables. a) public b) private c) protected d) static e) instance

D

Which of the following is a standard I/O stream? a. System.err b. System. in c. System.out d. All the above

D

Given the following code segment: where A is an interface, B is a concrete class with a default constructor that implements A. interface A{ } class B implements A{ } Which of the following is correct? a) A a = new A(); b) A a = new B(); c) B b = new A(); d) B b = new B(); e) b and d

E

The expression "Maddie" + 1 + 2 + 3 evaluates to _____________?

Maddie123

Does System.out.println('Welcome to Java'); compile?

No

Is main a reserved word?

No

Is this a java comment? --comment

No

When an object of a child class is assigned to a parent type reference variable, do you have access to the methods in both the parent and child classes without casting?

No

If x is the String "Hi There", then x.toUpperCase().toLowerCase(); will it return the original version of x.

No it will return "hi there"

A ________ class diagram shows the relationships between classes in a project. a. JDK b. IDE c. UML d. ASCII e. None of the above.

ULM

What is the difference between LAN and WAN network?

WAN connects two or more LANs

class Short1{ public static void main(String[] args) { String s1 = new String("Welcome to Java!"); String s2 = s1.toUpperCase(); System.out.println (s2); int index = s1.indexOf(" "); String s3 = s1.substring (0, index); System.out.println (s3); s3 = s3.concat ("!"); System.out.println (s3); System.out.println (s1.length()); } }

WELCOME TO JAVA! Welcome Welcome! 16

Are all object references in Java potentially polymorphic?

Yes

Can methods be abstract?

Yes

Does System.out.println("Welcome to Java"); compile?

Yes

Given the declaration Account x = new Account(). Does x contain a reference to a Account object?

Yes

Is class a reserved word?

Yes

Is float a reserved word?

Yes

Is int a reserved word?

Yes

Is void a reserved word?

Yes

Since all classes are derived from the Object class, do all classes have an is-a relationship with the Object class.

Yes

On which of the following line is the toString() method of the client object invoked implicitly? a. System.out.print(client); b. Client client = new Client("Adrian Beltre"); c. String name = client.getName(); d. Double.parseDouble(client.getNumber()); e. None of the above invokes a toString method implicitly.

a

which one is declaring and which one is initializing? a. int var1 = 3; b. int var2;

a. var1 is initializing and b. var2 is declaring.

Name byte long int and short from smallest to largest

byte short int long

Analyze the following code: public class Test { public static void main(String[] args) { double radius; final double PI= 3.15169; double area = radius x radius x PI; System.out.println("Area is " + area); } } Why does this code have a compile-time error?

because the variable radius is not initialized

Which of the following is an invalid identifier and why? a. funny$ b. num_2 c. num2 d. book-mark e. _num

d. book-mark because you cannot have a "-" in your identifier.

How do you declare multiple variables the same type?

double hours, weeks;

A(n) ________ method changes the ________ of the object. a. accessor, state b. state, accessor c. mutator, behaviors d. static, state e. mutator, state

e

Consider the following method header: public boolean spend(double amountSpent) Complete the code below to call the spend method: ________ x = customer.spend(userInput); a. public b. double c. void d. int e. None of the above.

e

The statement below provides a program access to the _________ class in the _________ package. import java.io.PrintWriter; a. io, java b. java.io, PrintWriter c. Writer, java.io.Print d. java.io.Print, Writer e. PrintWriter, java.io

e

What is the value of x after the following code is executed? int x = 14 % 6; a. 0 b. 2.333333 c. 20 d. 1 e. 2

e

What is the value of y after the following code is executed? double y = 7 - 3 / 3 - 2; a. -2.0 b. -1.0 c. 0.0 d. 2.0 e. 4.0

e

Which of the following instance fields violates encapsulation? a. public static final int XMAX = 10; b. private static final int POINTS = 1052; c. private boolean isIt = true; d. private double weight; e. None of the above violates encapsulation.

e

Which of the following lines of code modifies the String object referenced by the variable strInput? a. int total = strInput.length(); b. return strInput.replace("a", "@"); c. System.out.println("Input: " + strInput + "\n"); d. double x = Double.parseDouble(strInput); e. None of the above.

e

A(n) ________________ is an objective that describes an unusual or erroneous situation

exception

which reserved word (modifier) should be used to prevent a method from being overridden in subclasses?

final


Related study sets

American Government: System of Government

View Set

Compound Subjects and Predicates - PRACTICE

View Set

Goal-setting and "SMART" Goals for Health

View Set

Chapter 6 Quiz - Management Information Systems

View Set