CS Unit 2
Note that indexOf returns __ if the string isn't found
-1
Returns 0 if the strings are the same Returns a negative value if this string comes before otherString alphabetically Returns a positive value if this string comes after otherString alphabetically
.compareTo (String otherString)
What string method tests if data stored in otherString is the same as this one and returns true or false
.equals (String otherString);
What string method returns the index of the first occurrence of str in the calling String
.indexOf(String str)
What string method returns an int, the number of chars in the string
.length
Returns the substring from the specified index to the end
.substring (int firstoneyouwant)
Returns the substring from the first int up to but not including the second
.substring(int firstoneyouwant, int firstoneyoudnotwant)
Transforms the string to lower case
.toLowerCase
Transforms the string to uppercase
.toUpperCase
primitive data types can hold how many pieces of information at a time?
1
a.length() will return ______, when the last index is 5
6
If we wanted to store an int in its wrapper class, we simply instantiate a new object of type Integer as follows:
Integer x = new Integer(45);
If I want to find absolute value
Math.abs(double num)
If I want to find powers and exponents
Math.pow(double base, double exponent)
If I want to generate random numbers
Math.random()
If I want to find the square root of a value
Math.sqrt(double num)
_______ ______ return a value, which we capture using a variable or use in an expression like a print statement:
Non void methods
To include a literal quote in a String use To include a new line in a String: \n
\"
To include a literal single quote in a String use
\'
To include a literal backslash in a String use
\\
To include a new line in a String use
\n
To include a tab in a String use
\t
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. a) AP CSA b) AP AP c) CSA AP d) CSA CSA
a
String w1; String w2 = "apple"; String w3 = "banana"; What is stored in w1 by the following? w1 = w2 + " sauce"; a) apple sauce b) sauce c) apple d) applesauce e) Nothing, + does not work on strings
a
String w1; String w2 = "apple"; String w3 = "banana"; What is the output? System.out.println(w2.compareTo(w3)); a) -1 b) error c) 1 d) 2 e) 0
a
Suppose s is a string, and that the call s.indexOf("an") returns 3. Which of the following could be the value of s? a) banana b) breathing c) anachronism d) advance e) meaning
a
The correct way to concatenate two Strings is ______. a) Using the + symbol between them b) Using the - symbol between them c) None of the items listed. d) Using a comma between them e) Using the String method merge
a
Which of the following best describes the relationship between a class and an object? a) A class is a template or blueprint from which an object can be created. b) A class is a piece of data which is contained in an object. c) An object is used to create classes, which are primitive data types. d) A class is a specific variable in a program, which has behaviors defined by objects.
a
Which of the following is not a wrapper class? a) String b) Integer c) Double
a
Which of the following is true about classes and objects? a) Object variables have a reference pointing to their location in memory. b) A class is an instance of an object. c) It is possible to have many classes of the same object. d) Objects only contain one piece of data e) Classes and object are the same exact thing and the terms can be used interchangeably.
a
Which of the following would set the String variable firstName to be completely empty (i.e. not even storing a memory address)? a) String firstName = null; b) String firstName = 0; c) String firstName = " "; d) String firstName = ""; e) String firstName = firstName;
a
Which of the following are true about class data types? Click all that apply. a) a programmer can create them b) has built in methods (tools) c) holds more than one piece of data at a time d) an example is int
abc
Which of the following code segments show examples of autoboxing? Check all that apply a) String s = "a string"; Integer len = s.length(); b) Double a = new Double(17.5); int compareValue = a.compareTo(20.0); c) Double d = 15.0; d) Integer i = new Integer(15); System.out.print("Answer: " + i); e) Double d = new Double(5.0);double e = d - 1.5;
abc
Which of the following is part of the signature of a method? Check all that apply a) number of parameters b) method name c) return type d) type of parameters e) order of parameters
abde
The values we pass in as parameters are often referred to as ________ __________ , as opposed to the formal parameters defined in the method signature.
actual parameters
When Java is given an int value where it was expecting an Integer value, it can perform a process called ______ which automatically converts the int value into an Integer.
autoboxing
According to the Java Quick reference Guide which of the following Math functions do you NOT need to know for the AP exam? a) random b) max c) sqrt d) abs e) pow
b
String w1; String w2 = "apple"; String w3 = "banana"; What is stored in w1 by the following? w1 = w3.substring(1,3); a) ana b) an c) na d) ban e) ba
b
String w1; String w2 = "apple"; String w3 = "banana"; What is stored in w1 by the following? w1 = w3.substring(2); a) n b) nana c) a d) ana e) anana
b
String w1; String w2 = "apple"; String w3 = "banana"; What is the output? System.out.println(w3.compareTo("fruit")); a) 3 b) -4 c) 4 d) -3.5 e) error
b
Which code shows us creating an object of data type Scanner class and storing it with a variable named X? a) X Scanner = new Scanner(System.in); b) Scanner X = new Scanner(System.in); c) new Scanner X = new Scanner(System.in); d) X = new Scanner(System.in); e) Scanner X = Scanner(System.in);
b
Which of the following will be printed by the following code? RegularPolygon shape = new RegularPolygon(5); System.out.println(shape); a) square with side length 5.0 b) regular pentagon with side length 1.0 c) regular pentagon with side length 5.0 d) An error occurs as not enough parameters are used in the constructor. e) equilateral triangle with side length 5.0
b
Which package do the wrapper classes belong to? a) No package. b) java.lang c) java.util d) shapes
b
All escape sequences start with a
backslash
Select all the classes from the list a) double b) String c) int d) Scanner e) char f) myWord g) 7.5 h) "Hello"
bd
______ are determined by the methods available in the class.
behaviors
__________ are the actions which the object can perform.
behaviors
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? a) setRadius(circ1) = 6.5; b) Circle.setRadius(6.5); c) circ1.setRadius(6.5); d) Circle.setRadius(circ1, 6.5); e) circ1.getRadius() = 6.5;
c
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? a) c.compareTo(c) b) It is not possible for the compareTo method to return a negative number c) b.compareTo(a) d) a.compareTo(b) e) a.compareTo(c)
c
The Math.pow() method returns a value that is of the ______ data type. a) String b) double c) boolean d) int
c
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"); a) \s b) \n c) \t d) \\ e) Any of the options listed
c
Where can a description of the accessible methods for a class be found? a) In the .class file b) In the output produced by running the class c) In the API documentation for the relevant library
c
Which of the following code segments contains a static method call? a) Scanner s = new Scanner(System.in); String l = s.nextLine(); b) Circle c = new Circle(5.0); double a = c.getArea(); c) double a = 1.5; double b = Math.pow(a, 4); d) int a = Integer.MAX_VALUE; a--; e) Rectangle r = new Rectangle(); r.setWidth(20.5);
c
Which of these code segments shows a constructor being used to create a new object? a) RegularPolygon p = null; b) Rectangle r; c) Circle c = new Circle(3.8); d) System.out.print("new Rectangle"); e) double num = 2.65;
c
The parameters are passed to the constructor using ___ ___ ____ This means our constructor gets a copy of that value not the actual value itself, or a copy of the reference to that value if it is reference data (e.g. a String).
call by value
Being immutable means once we set up a string in memory we (can/cant) change it
cant
Which of the following describe situations where unboxing occurs? Choose all options that apply. a) When a wrapper class object is initialized using a constructor with a primitive parameter b) When a method is called on a wrapper class object c) When a wrapper class object is assigned to a variable of the corresponding primitive type d) When a wrapper class object is passed to a method that expects a value of the corresponding primitive type
cd
When strings are stored in memory, they're stored as a series of
chars
Circle c = new Circle(3.5); System.out.println(c); What will it print out?
circle with radius 3.5
A ____ itself is essentially the template for an object.
class
______ data types can hold several pieces of data and have methods to work on that data
class
______ data types hold the memory address of where the data is stored.
class
the datatype of the object is a ______
class
String addition is also called
concatenation
What is output to the screen by the following code? System.out.println("Sum=" + 4 + 5); a) Sum=54 b) Sum=9 c) Sum=4 5 d) Sum=45 e) Sum= 5 4
d
What is wrong with: int s = Math.sqrt(49); a) Math.sqrt cannot take a number, it must be a variable. b) Nothing, the code is correct as written. c) The 49 should be 49.0 since Math.sqrt takes a double type parameter. d) It needs a numeric cast since Math.sqrt returns a double data type. e) Math.sqrt is void and does not return a value.
d
Which of the following statements about overloaded constructors is true? a) Constructors cannot be overloaded. Only one constructor may exist in each class. b) Constructors for a class can be overloaded but they must have different numbers of parameters. c) Constructors for a class can be overloaded but they must all have different names. d) 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.
d
Which of the following creates a rectangle with length and width equal to 4.5? Choose all answers that apply. a) Rectangle r = new Rectangle(4.5, 4.5, 4.5, 4.5); b) Rectangle r = new Rectangle(); c) Rectangle r = new Rectangle(4.5, 1.0); d) Rectangle r = new Rectangle(4.5); e) Rectangle r = new Rectangle(4.5, 4.5);
de
How to edit the range of Math.random?
double ran = Math.random(); int range = 100; int min = 1; double answer = (int) (ran * range) + min;
Consider the following code: String major = "Computer Science"; What is returned by the method call major.substring(1, 2)? a) "Co" b) "C" c) The call causes an error d) 'om' e) "o"
e
Suppose s is a String which contains 5 characters. Which of the following calls will cause an error? a) s.substring(4, 5) b) s.substring(0) c) All the calls shown will cause an error d) s.substring(0, 5) e) None of the calls shown will cause an error
e
What is stored in w1 by the following? w1 = w2.toUpperCase(); a) error b) applE c) Apple d) apple e) APPLE
e
What will be printed by the following code? System.out.print("All\nright\tnow"); a) Allnrighttnow b) Allrightnow c) All right now d) All right now e) All right now
e
Which of the following correctly declares a new variable which can be used to point to an object of the String class type? a) new String; b) str String; c) str = String; d) String "str"; e) String str;
e
Which of the following symbols need an escape character to be output in a System.out.print statement? a) / b) $ c) % d) ; e) \
e
Consider the following code: int x = 56;int y = 30;System.out.print("Answer: " + x + y); This code outputs: Answer: 86 True or False?
f
The parameter list part of the signature is not only the number of parameters, but also the types and variable names; these are referred to as the ________________ ____________________
formal parameters
the String class is what we call
immutable
When using .substring, trying to access a letter outside of the range 0 to length -1 will result in an ______________ error
index out of bounds
different packages exist within
libraries
Class data types, including strings, have ________, that allow us to change or report on the data stored in them.
methods
The signature is made up of the _____ (which in the case of a constructor is the same as the class name) and the ______ _____ for the class.
name, parameter list
Make sure you are putting the _____ of a String variable before the _____ _________________
name;dot operator
When creating an Object we use the word ____ when we declare the data type.
new
We call a class variable which contains no information
null
When two methods in a class have the same name they are said to be...
overloaded
different classes are divided into
packages
All we can do with a string is make the variable
point at a different location in memory
The two kinds of data types in Java are
primitive and class
When we declare a class data type, Java allocates a space in memory called a
reference
Java uses something called a _________ whenever a constructor or method appears in source code to identify what it actually has to call.
signature
An object in Java is something we can use in code that has both a
state and behaviors
A reference is a box that is named the variable name, but only holds the memory location of the actual data. It does this because the data could be large. (T/F)
t
Consider the following code: System.out.println("Answer: " + (2 + 5)); This code outputs: Answer: 7 True or False?
t
char is a primitive data type (t/f)?
t
What is a static method call?
the methods in Java that can be called without creating an object of class.
We call a method from a String variable, like this: String a = "string"; a.method(); True or false?
true
Converting an Integer value into an int is called
unboxing
_____ ____ perform a function, but don't give us any information back.
void methods
The behaviors of an object are determined by
what type of object it is