AP COMP SCI A -- UNIT 2

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Which code shows an example of autoboxing being used correctly? 1. Double m = 2.5; 2. Integer c = Integer((int)5.6); 3. Integer k = new Integer(7); 4.Double n = new Double(4.8); n += 3.5;System.out.println(n); 5.Integer b = new Integer(14); System.out.println(b.intValue());

1. Double m = 2.5;

Which of the following code segments correctly declare a String s and gives it a value of "fortran". 1. String s = "fortran"; 2. s = new String();s = "fortran"; 3. String s;s = "fortran"; 4. String s = ("fortran"); 5. String s = new "fortran"; 6. s = new String();String s = "fortran";

1. String s = "fortran"; & 3. String s;s = "fortran";

Which one of the following sentences is true? 1.A String is a class data type so a String variable holds a reference to a location in memory 2.A String is a class data type so a String variable directly stores the data for the String. 3.A String is a primitive data type so a String variable directly stores the data for the String. 4.A String is a primitive data type so a String variable holds a reference to a location in memory

1.A String is a class data type so a String variable holds a reference to a location in memory

Which of the following best describes what happens when the code segment below is run? Circle c = new Circle();System.out.println(c); 1."null" is printed as the circle has no radius 2."circle with radius 0.0" is printed 3."circle with radius 1.0" is printed 4.An error occurs as the Circle constructor needs a parameter. 5.Nothing will be printed.

3."circle with radius 1.0" is printed

Which of the following best describes the relationship between a class and an object? 1.A class is a specific variable in a program, which has behaviors defined by objects. 2.A class is a piece of data which is contained in an object. 3.A class is a template or blueprint from which an object can be created. 4. An object is used to create classes, which are primitive data types.

3.A class is a template or blueprint from which an object can be created.

Which of the following code segments contains a static method call? 1. int a = Integer.MAX_VALUE; a--; 2.Rectangle r = new Rectangle(); r.setWidth(20.5); 3.double a = 1.5; double b = Math.pow(a, 4); 4.Scanner s = new Scanner(System.in); String l = s.nextLine(); 5.Circle c = new Circle(5.0); double a = c.getArea();

3.double a = 1.5; double b = Math.pow(a, 4);

Suppose the variable shape is declared and initialized as a regular polygon as follows: RegularPolygon shape = new RegularPolygon(6, 2.0); Which of the following will change shape to be a regular decagon (with 10 sides) and side-length 5.0? 1.shape.addSides(10); shape.setSideLength(5.0); 2.shape.setNumSides(4); shape.setSideLength(5.0); 3.shape.addSides(4); shape.setSideLength(5.0); 4.shape.addSides(4); shape.setSideLength(3.0);

3.shape.addSides(4);shape.setSideLength(5.0);

What would be printed by the following code? double y = 4.7; Integer n = new Integer((int) y); System.out.println(n);

4

int a = 20; int b = 8; System.out.println(a % b); What is output?

4

A RegularPolygon p is declared and instantiated as shown: RegularPolygon p = new RegularPolygon(5, 2.5); Which code example shows a non-void method correctly being called on p and its return value being stored using a variable of the correct type? 1.int n = getNumSides(p); 2.p.setNumSides(7); 3.p.getArea(); 4. double a = p.getArea(); 5.int s = p.addSides();

4. double a = p.getArea();

Which of the following lines of code create a String variable x and set it equal to the word Cat? 1. None of the lines of code listed. 2.x.toString("Cat"); 3.All of the lines of code listed. 4.String x = "Cat"; 5.(String) cat = x;

4.String x = "Cat";

Which of the following commands will print the output below? \/ ' ' 1. System.out.print("\/\n''") 2. System.out.print("\\/n''") 3. System.out.print("\\\/\n''") 4. System.out.print("\\\/\n\'\'); 5. System.out.print("\\/\n\'\'")

5. System.out.print("\\/\n\'\'")

Given the following code, what would be the value stored in the Double object referenced by the variable d? int x = 7; Double d = new Double(x);

7.0

Consider the following code: double x =-97.6; System.out.println((int) Math.abs(x)); What is output?

97

Consider the following code: String a = "CSA"; String b = a; a = "AP"; System.out.println(a + " " + b"); What is printed when the code is run? Hint: Remember Strings are immutable.

AP CSA

What is stored in w1 by the following? String w1; String w2 = "apple"; String w3 = "banana"; w1 = w2.toUpperCase();

APPLE

What will be printed by the following code? System.out.print("All\nright\tnow");

All right now

What is output by the following code? System.out.println("Hello, \n\tWorld");

Hello, World The output can be broken into the following parts: "Hello, " + "\n" + "\t" + "World". The "\n" represents a new line and the "\t" represents a tab character so the output will be the String "Hello," followed by a newline with a tab character followed by "World"

Where can a description of the accessible methods for a class be found?

In the API documentation for the relevant library

What is the maximum value an Integer can hold? - Integer.MAX_VALUE - There is no limit to the value an Integer can hold. - Integer.MaxValue() - Integer.Max()

Integer.MAX_VALUE

What is wrong with: int s = Math.sqrt(49);

It needs a numeric cast since Math.sqrt returns a double data type

Which of the following is not a method in the Integer wrapper class? toString equals intValue MAX_VALUE

MAX_VALUE

Which of the following creates a rectangle with length and width equal to 4.5? Choose all answers that apply. Rectangle r = new Rectangle(4.5, 1.0); Rectangle r = new Rectangle(4.5); Rectangle r = new Rectangle(4.5, 4.5, 4.5, 4.5); Rectangle r = new Rectangle(); Rectangle r = new Rectangle(4.5, 4.5);

Rectangle r = new Rectangle(4.5); Rectangle r = new Rectangle(4.5, 4.5);

Which of the following is a class in Java? Scanner main char int

Scanner

Which code shows us creating an object of data type Scanner class and storing it with a variable named X ? X Scanner = new Scanner(System.in); Scanner X = new Scanner(System.in); X = new Scanner(System.in); new Scanner X = new Scanner(System.in); Scanner X = Scanner(System.in);

Scanner X = new Scanner(System.in);

Which of the following is not a wrapper class? Double String Integer

String

Select all the classes from the list double char "Hello" 7.5 myWord int String Scanner

String Scanner

Which of the following code segments show examples of autoboxing? String s = "a string";Integer len = s.length(); Integer i = new Integer(15);System.out.print("Answer: " + i); Double d = new Double(5.0);double e = d - 1.5; Double d = 15; Double a = new Double(17.5);int compareValue = a.compareTo(20.0);

String s = "a string";Integer len = s.length(); Double a = new Double(17.5);int compareValue = a.compareTo(20.0); Double d = 15;

What is output to the screen by the following code? System.out.println("Sum=" + 4 + 5);

Sum=45 The + is interpreted as a string concatenation not an int addition when it is run

Consider the following code: System.out.println("Answer: " + (2 + 5)); This code outputs: Answer: 7 TRUE OR FALSE

TRUE

A method which returns no value is referred to as...

void

The correct way to concatenate two Strings is ______.

Using the + symbol between them

The Math.pow() method returns a value that is of the ______ data type.

double

How would you declare a variable x of type double and set it equal to 25.25?

double x = 25.25;

Which of the following are examples of packages you can import and use classes from in java? Choose all options that apply. Main.main java.double java.int edhesive.shapes java.util

edhesive.shapes java.util

uppose the String s is declared and initialized as follows: String s = "elementary"; What String would be returned by the method call s.substring(2, 6);?

emen

Which of the following is true about classes and objects? 1.It is possible to have many classes of the same object. 2.Classes and object are the same exact thing and the terms can be used interchangeably. 3.A class is an instance of an object. 4.Objects have a reference pointing to their location in memory. 5.Objects only contain one piece of data

is that objects have a reference pointing to their location in memory

Which package do the wrapper classes belong to?

java.lang

According to the Java Quick reference Guide which of the following Math functions do you NOT need to know for the AP exam? random pow max sqrt abs

max

Which of the following is part of the signature of a method? return type method name type of parameters number of parameters order of parameters

method name type of parameters number of parameters order of parameters

What is stored in w1 by the following? String w1; String w2 = "apple"; String w3 = "banana"; w1 = w3.substring(2);

nana

What keyword do we use to tell Java to set aside memory to create an object?

new

What keyword is used in order to build an object in memory of any data type (String, Scanner, etc)?

new

A special value that can be assigned to an object that has no value:

null

A special value that means "no object" is called ______.

null

Consider the following code: String major = "Computer Science"; What is returned by the method call major.substring(1, 2)?

o

When two methods in a class have the same name they are said to be...

overloaded

---- data types hold only one piece of information at a time.

primitive

What does the following code do? String w3 = "aardvark"; System.out.println(w3.substring(w3.length()-2));

rk

Suppose s is a String which contains 5 characters. Which of the following calls will not cause an error? s.substring(5) s.substring(0, 5) All the calls shown will cause an error None of the calls shown will cause an error s.substring(5, 5)

s.substring(0,5)

Suppose s1 and s2 are Strings and the following code is run: s1 = s1.toLowerCase(); s2 = s2.toLowerCase(); System.out.println(s1.compareTo(s2)); If the code causes -25 to be output, which of the following must be true

s1 appears before s2 alphabetically

----- data types can hold several pieces of data and have methods to work on that data.

class

Consider the following code: String a = "credulous"; String b = "differently"; System.out.println(a + b); What is output?

credulousdifferently

What is the output? String w1; String w2 = "apple"; String w3 = "banana"; System.out.println(w2.compareTo(w3));

-1

What will be printed by the following code? String s = "Animal"; System.out.println(s.indexOf("an"));

-1

What is the output? String w1; String w2 = "apple"; String w3 = "banana"; System.out.println(w3.compareTo("fruit"));

-4

Which of the following statements about overloaded constructors is true? 1.Constructors for a class can be overloaded but they must all have a different number of parameters, different order of parameters or different types of parameters. 2.Constructors for a class can be overloaded but they must all have different names. 3.Constructors cannot be overloaded. Only one constructor may exist in each class. 4.Constructors for a class can be overloaded but they must have different numbers of parameters.

1.Constructors for a class can be overloaded but they must all have a different number of parameters, different order of parameters or different types of parameters.

A class ExampleClass is imported into Java. Which of the following could be a correct signature for a constructor for this class? 1.ExampleClass(int param1, String param2) 4.constructorEC() 2.ExampleConstructor(int param1) 3.ExampleClassMaker(double param1, double param2)

1.ExampleClass(int param1, String param2)

Which of the following are signatures for constructors in the Rectangle class? Choose all options that apply. 1.Rectangle(double len) 2.Rectangle() 3.Rectangle(int len) 4.Rectangle(int len, int wid) 5.Rectangle(double len, double wid) 6.Rectangle(double len1, double len2, double len3, double len4)

1.Rectangle(double len) 2.Rectangle() 5.Rectangle(double len, double wid)

Which of the following lines of code could be used to create a regular polygon with 7 sides of length 1.0? Choose all options that apply. 1.RegularPolygon rp = new RegularPolygon(7) 2.RegularPolygon rp = new RegularPolygon() 3.RegularPolygon rp = new RegularPolygon(1.0) 4.RegularPolygon rp = new RegularPolygon(7, 1.0) 5.RegularPolygon rp = new RegularPolygon(7.0) 6.RegularPolygon rp = new RegularPolygon(1.0, 7)

1.RegularPolygon rp = new RegularPolygon(7) 4.RegularPolygon rp = new RegularPolygon(7, 1.0)

Which of the following would set the String variable firstName to be completely empty (i.e. not even storing a memory address)? 1.String firstName = null; 2.String firstName = ""; 3.String firstName = " "; 4.String firstName = 0; 5.String firstName = firstName;

1.String firstName = null;

How could you create a String variable, s, and set it equal to "Hi There" in one line? 1.String s = "Hi There"; 2.String s = ("Hi There"); 3.s = "Hi There"; 4.s = new String("Hi There");

1.String s = "Hi There";

Suppose the variable circ1 is declared and instantiated as follows: Circle circ1 = new Circle(4.0); Which of the following will change the radius of circ1 to 6.5? 1.circ1.setRadius(6.5); 2.Circle.setRadius(circ1, 6.5); 3.Circle.setRadius(6.5); 4.circ1.getRadius() = 6.5; 5.setRadius(circ1) = 6.5;

1.circ1.setRadius(6.5);

What is the output when the following code is executed? Rectangle r1 = new Rectangle(2.0); r1.setLength(4.0); System.out.println(r1.getPerimeter());

12.0

Consider the following code: int x = 9; int y = 8; System.out.println(x + y); What is output?

17

Which of the following will be printed by the following code? RegularPolygon shape = new RegularPolygon(5); System.out.println(shape); 1.equilateral triangle with side length 5.0 2.regular pentagon with side length 1.0 3.square with side length 5.0 4.An error occurs as not enough parameters are used in the constructor. 5.regular pentagon with side length 5.0

2. regular pentagon with side length 1.0

Which of the following correctly declares a new variable which can be used to point to an object of the String class type? 1.str String; 2.String str; 3.str = String; 4.String "str"; 5.new String;

2.String str;

Which of the following are true about class data types? Click all that apply. 1. an example is int 2.a programmer can create them 3.has built in methods (tools) 4. holds more than one piece of data at a time

2.a programmer can create them 3.has built in methods (tools) 4. holds more than one piece of data at a time

The variable r1 is declared and initialized as a Rectangle as follows. Rectangle r1 = new Rectangle(3.0, 5.0); Which of the following correctly calls the Rectangle getArea method on r1? 1.r1.getArea(Rectangle); 2.r1.getArea(); 3.Rectangle.getArea(r1); 4.getArea(r1);

2.r1.getArea();

Select all the primitive data types. Rectangle Scanner boolean int RegularPolygon String double Circle

boolean int double

Which of these code segments shows a constructor being used to create a new object? Rectangle r; double num = 2.65; RegularPolygon p = null; Circle c = new Circle(3.8); System.out.print("new Rectangle");

Circle c = new Circle(3.8);

Consider the following code segment: Scanner scan = new Scanner(System.in); What does this code segment do?

Declares and initializes an object of the Scanner class

Consider the following code: int x = 56; int y = 30; System.out.print("Answer: " + x + y); This code outputs: Answer: 86 TRUE OR FALSE

FALSE

--- data types hold the memory address of where the data is stored.

class

Consider the following code example: String word = new String("answer"); Which of the following sentences best describes the features in this code segment? Variable String points to an object, answer is the class of this object Variable word points to an object, answer is the class of this object Variable String points to an object, word is the class of this object Variable word points to an object, String is the class of this object Variable answer points to an object, word is the class of this object

Variable word points to an object, String is the class of this object

Which of the following describe situations where unboxing occurs? Choose all options that apply. When a method is called on a wrapper class object When a wrapper class object is passed to a method that expects a value of the corresponding primitive type When a wrapper class object is initialized using a constructor with a primitive parameter When a wrapper class object is assigned to a variable of the corresponding primitive type

When a wrapper class object is passed to a method that expects a value of the corresponding primitive type When a wrapper class object is assigned to a variable of the corresponding primitive type

Which of the following symbols need an escape character to be output in a System.out.print statement?

\ ; Correct! Backslash is used to escape special characters, so you have to escape it also with \

To output: Nums: 12 13 14 15 You would need to use what escape sequence in the blank: System.out.print("Nums: 12 13 14______15");

\t; Correct! \t prints a tab character which makes a large amount of space when displayed. Tabs are usually equal to 8 spaces, but it can depend on the software and font that is being used to display them.

Suppose s is a string, and that the call s.indexOf("an") returns 3. Which of the following could be the value of s? meaning banana advance anachronism breathing

advance because A = 0, d = 1, v= 2, a = 3, n = 4

What is stored in w1 by the following? String w1; String w2 = "apple"; String w3 = "banana"; w1 = w3.substring(1,3); -

an

Use the following code for questions 2-7: String w1; String w2 = "apple"; String w3 = "banana"; What is stored in w1 by the following? w1 = w2 + " sauce";

apple sauce

Suppose three strings are declared and initialized as shown below. String a = "lizard"; String b = "chameleon"; String c = "lizard"; Which of the following calls to the compareTo method will return a negative number?

b.comparedTo(a)


Kaugnay na mga set ng pag-aaral

AP Macroeconomics Module 11: Interpreting Real Gross Domestic Product

View Set

Mgmt Info Systems: Exam 1 Review

View Set

Five Principles for Communication

View Set

Ch 6 The Business Plan: Visualizing the Dream

View Set

Language Arts 09, Section - 4 Modules - Adi Final

View Set

Embryology (Gray's Anatomy Review & Lippincott & BRS)

View Set

Chapter 8: Preliminary examination

View Set