Deck 3: Using Classes and Objects

ملء الشاشة (f)
exit full mode
سؤال
Which of the following is a correct declaration of enumerated type for the suits of a deck of cards?

A) enumerated type Suit = { hearts, spades, diamonds, clubs };
B) enum Suit {hearts, spades, diamonds, clubs };
C) enum Suit {hearts, spades, diamonds, clubs }
D) enumerated type Suit = {hearts, spades, diamonds, clubs };
E) enum Suit = { hearts, spades, diamonds, clubs }
استخدم زر المسافة أو
up arrow
down arrow
لقلب البطاقة.
سؤال
Suppose we have a String object referenced by a variable called listing. Suppose we want a new String object that consists of the first 5 characters in listing. Which of the following lines of code will achieve this?

A) String prefix = listing.front(5);
B) String prefix = listing.front(6);
C) String prefix = listing.substring(1,5);
D) String prefix = listing.substring(0,5);
E) String prefix = listing.firstChars(5);
سؤال
Assume that we have a Random object referenced by a variable called generator. Which of the following lines will generate a random number in the range 5-20 and store it in the int variable randNum?

A) randNum = generator.nextInt(15) + 5;
B) randNum = generator.nextInt(15) + 6;
C) randNum = generator.nextInt(16) + 5;
D) randNum = generator.nextInt(16) + 6;
E) none of the above
سؤال
When there are no references to an object, the object is marked as garbage and is automatically removed from memory using Java's automatic garbage collection feature.
سؤال
Which of the following classes include the getCurrencyInstance() method?

A) String
B) NumberFormat
C) DecimalFormat
D) Math
E) none of the above
سؤال
When an object variable is declared but it is not assigned a value, it is called a ______________________.

A) null reference
B) empty reference
C) void reference
D) zero reference
E) static reference
سؤال
Which of the following expressions correctly compute 5 + 26?

A) result = 5 + 2^6;
B) result = 5 + 2*exponent(6);
C) result = 5 + 2*Math.exponent(6);
D) result = 5 + Math.pow(2, 6);
E) none of the above
سؤال
String objects can be changed after instantiation.
سؤال
Multiple reference variables can refer to the same object.
سؤال
A special method that is invoked to set up an object during instantiation is called a ___________________.

A) new method
B) dot operator
C) creator
D) constructor
E) destructor
سؤال
Which of the following represents the proper way to create a NumberFormat object that formats numbers as percentages?

A) NumberFormat fmt = new NumberFormat(%);
B) NumberFormat fmt = new NumberFormat("%");
C) NumberFormat fmt = NumberFormat.getPercentInstance();
D) NumberFormat fmt = new PercentNumberFormat();
E) none of the above
سؤال
A variable that is declared as a primitive type stores the actual value of the associated data. A variable that is declared as an object type stores a pointer to the associated object.
سؤال
____________________ is the automatic conversion between a primitive value and a corresponding wrapper object.

A) Generating
B) Aliasing
C) Number formatting
D) Static invocation
E) Autoboxing
سؤال
Which of the following is an invalid way to instantiate a String object?

A) String title = new String("Java Software Solutions");
B) String name = "John Lewis";
C) String empty = "";
D) String alsoEmpty = new String("");
E) all of the above are valid
سؤال
The new operator is used to access an objects methods.
سؤال
The String class _______________________________ .

A) is part of the java.lang package.
B) is part of the java.util.package.
C) is a wrapper class.
D) none of the above.
E) all of the above.
سؤال
The ________________ operator is used to instantiate an object.

A) static
B) new
C) +
D) -
E) none of the above
سؤال
Which of the following best describes what happens when an object no longer has any references pointing to it?

A) The object is overwritten the next time the new operator is called.
B) The object is overwritten the next time the new operator is called using the same class.
C) The object is immediately deleted from memory.
D) The object is marked as garbage and its associated memory is freed when the garbage collector runs.
E) The object stays in memory for the remainder of the programs execution.
سؤال
Consider the following snippet of code: Random generator = new Random();
Int randNum = generator.nextInt(20) + 1;
Which of the following will be true after these lines are executed?

A) randNum will hold a number between 1 and 20 inclusive.
B) randNum will hold a number between 0 and 20 inclusive.
C) randNum will hold a number between 1 and 21 inclusive.
D) these lines will not be executed because a compiler error will result.
E) none of the above
سؤال
When two references point to the same object, ________________________________ .

A) a run-time error will occur.
B) a compiler error will occur.
C) the references are called aliases of each other.
D) the object will be marked for garbage collection.
E) the references are called null references.
سؤال
Write a short program that asks the user to input a string, and then outputs the number of characters in the string.
سؤال
Write a single statement that creates a DecimalFormat object that formats numbers to 2 decimal places.
سؤال
When called with integer parameter n, the nextInt() method of the Random class will return a randomly generated integer between 0 and n.
سؤال
The Math class is part of the java.lang package.
سؤال
The System.out.printf() method is an alternative way to output information in Java.
سؤال
All of the classes contained in the java.util package are automatically included in every Java program.
سؤال
Suppose a Random object has been created called generator. Write a single statement that generates a random number in the range 73 to 100.
سؤال
Write a declaration for an enumerated type that represents the months of the year.
سؤال
Suppose we have a String object called myString. Write a single line of Java code that will output myString in such a way that all of its characters are uppercase.
سؤال
What is the range of integers that will be generated by the following expression?
generator.nextInt(15) + 5;
سؤال
Explain what it means for a String object to be immutable. Are there any workarounds for this?
سؤال
Write a short program that allows the user to enter the base and height of a triangle and outputs the hypotenuse, formatted to three decimal places.
سؤال
Explain how variables representing objects and variables representing primitive types are different.
سؤال
Write an expression that will compute the length of the tangent of a right triangle, given one of the non-right angles. You may assume that this value is stored in a variable called angle.
سؤال
What is output by the following code fragment?
String hello = new String("Hello World!");
hello = hello.replace('H', 'W');
hello = hello.replace('W', 'H');
System.out.println(hello);
سؤال
Write an expression that computes 12 raised to the power 4.3 and store the result in a double called result.
سؤال
Write a single line that creates a wrapper for an int variable num.
سؤال
Write a short program that allows the user to input a positive integer and then outputs a randomly generated integer between 1 and the input number.
سؤال
Enumerated types allow a programmer to treat primitive data as objects.
سؤال
Write a statement that computes the square root of a variable called discriminant and stores the value in a variable called result.
فتح الحزمة
قم بالتسجيل لفتح البطاقات في هذه المجموعة!
Unlock Deck
Unlock Deck
1/40
auto play flashcards
العب
simple tutorial
ملء الشاشة (f)
exit full mode
Deck 3: Using Classes and Objects
1
Which of the following is a correct declaration of enumerated type for the suits of a deck of cards?

A) enumerated type Suit = { hearts, spades, diamonds, clubs };
B) enum Suit {hearts, spades, diamonds, clubs };
C) enum Suit {hearts, spades, diamonds, clubs }
D) enumerated type Suit = {hearts, spades, diamonds, clubs };
E) enum Suit = { hearts, spades, diamonds, clubs }
C
Explanation: Choice c represents the correct syntax for declaring an enumerated type called Suit that can take one of the values hearts, spades, diamonds or clubs.
2
Suppose we have a String object referenced by a variable called listing. Suppose we want a new String object that consists of the first 5 characters in listing. Which of the following lines of code will achieve this?

A) String prefix = listing.front(5);
B) String prefix = listing.front(6);
C) String prefix = listing.substring(1,5);
D) String prefix = listing.substring(0,5);
E) String prefix = listing.firstChars(5);
D
Explanation: Choices a, b, and e are wrong because the String class does not have methods called front or firstChars. Choice c is incorrect because the first character in the string is indexed by 1. Choice c is correct, since it will create a new string from the first 5 characters of listing.
3
Assume that we have a Random object referenced by a variable called generator. Which of the following lines will generate a random number in the range 5-20 and store it in the int variable randNum?

A) randNum = generator.nextInt(15) + 5;
B) randNum = generator.nextInt(15) + 6;
C) randNum = generator.nextInt(16) + 5;
D) randNum = generator.nextInt(16) + 6;
E) none of the above
C
Explanation: When called with 16 as a parameter, the nextInt() method will return a number in the range 0 to 15. Adding 5 to this random number will generate a number in the range 5 to 20.
4
When there are no references to an object, the object is marked as garbage and is automatically removed from memory using Java's automatic garbage collection feature.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
5
Which of the following classes include the getCurrencyInstance() method?

A) String
B) NumberFormat
C) DecimalFormat
D) Math
E) none of the above
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
6
When an object variable is declared but it is not assigned a value, it is called a ______________________.

A) null reference
B) empty reference
C) void reference
D) zero reference
E) static reference
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
7
Which of the following expressions correctly compute 5 + 26?

A) result = 5 + 2^6;
B) result = 5 + 2*exponent(6);
C) result = 5 + 2*Math.exponent(6);
D) result = 5 + Math.pow(2, 6);
E) none of the above
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
8
String objects can be changed after instantiation.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
9
Multiple reference variables can refer to the same object.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
10
A special method that is invoked to set up an object during instantiation is called a ___________________.

A) new method
B) dot operator
C) creator
D) constructor
E) destructor
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
11
Which of the following represents the proper way to create a NumberFormat object that formats numbers as percentages?

A) NumberFormat fmt = new NumberFormat(%);
B) NumberFormat fmt = new NumberFormat("%");
C) NumberFormat fmt = NumberFormat.getPercentInstance();
D) NumberFormat fmt = new PercentNumberFormat();
E) none of the above
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
12
A variable that is declared as a primitive type stores the actual value of the associated data. A variable that is declared as an object type stores a pointer to the associated object.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
13
____________________ is the automatic conversion between a primitive value and a corresponding wrapper object.

A) Generating
B) Aliasing
C) Number formatting
D) Static invocation
E) Autoboxing
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
14
Which of the following is an invalid way to instantiate a String object?

A) String title = new String("Java Software Solutions");
B) String name = "John Lewis";
C) String empty = "";
D) String alsoEmpty = new String("");
E) all of the above are valid
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
15
The new operator is used to access an objects methods.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
16
The String class _______________________________ .

A) is part of the java.lang package.
B) is part of the java.util.package.
C) is a wrapper class.
D) none of the above.
E) all of the above.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
17
The ________________ operator is used to instantiate an object.

A) static
B) new
C) +
D) -
E) none of the above
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
18
Which of the following best describes what happens when an object no longer has any references pointing to it?

A) The object is overwritten the next time the new operator is called.
B) The object is overwritten the next time the new operator is called using the same class.
C) The object is immediately deleted from memory.
D) The object is marked as garbage and its associated memory is freed when the garbage collector runs.
E) The object stays in memory for the remainder of the programs execution.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
19
Consider the following snippet of code: Random generator = new Random();
Int randNum = generator.nextInt(20) + 1;
Which of the following will be true after these lines are executed?

A) randNum will hold a number between 1 and 20 inclusive.
B) randNum will hold a number between 0 and 20 inclusive.
C) randNum will hold a number between 1 and 21 inclusive.
D) these lines will not be executed because a compiler error will result.
E) none of the above
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
20
When two references point to the same object, ________________________________ .

A) a run-time error will occur.
B) a compiler error will occur.
C) the references are called aliases of each other.
D) the object will be marked for garbage collection.
E) the references are called null references.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
21
Write a short program that asks the user to input a string, and then outputs the number of characters in the string.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
22
Write a single statement that creates a DecimalFormat object that formats numbers to 2 decimal places.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
23
When called with integer parameter n, the nextInt() method of the Random class will return a randomly generated integer between 0 and n.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
24
The Math class is part of the java.lang package.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
25
The System.out.printf() method is an alternative way to output information in Java.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
26
All of the classes contained in the java.util package are automatically included in every Java program.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
27
Suppose a Random object has been created called generator. Write a single statement that generates a random number in the range 73 to 100.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
28
Write a declaration for an enumerated type that represents the months of the year.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
29
Suppose we have a String object called myString. Write a single line of Java code that will output myString in such a way that all of its characters are uppercase.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
30
What is the range of integers that will be generated by the following expression?
generator.nextInt(15) + 5;
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
31
Explain what it means for a String object to be immutable. Are there any workarounds for this?
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
32
Write a short program that allows the user to enter the base and height of a triangle and outputs the hypotenuse, formatted to three decimal places.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
33
Explain how variables representing objects and variables representing primitive types are different.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
34
Write an expression that will compute the length of the tangent of a right triangle, given one of the non-right angles. You may assume that this value is stored in a variable called angle.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
35
What is output by the following code fragment?
String hello = new String("Hello World!");
hello = hello.replace('H', 'W');
hello = hello.replace('W', 'H');
System.out.println(hello);
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
36
Write an expression that computes 12 raised to the power 4.3 and store the result in a double called result.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
37
Write a single line that creates a wrapper for an int variable num.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
38
Write a short program that allows the user to input a positive integer and then outputs a randomly generated integer between 1 and the input number.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
39
Enumerated types allow a programmer to treat primitive data as objects.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
40
Write a statement that computes the square root of a variable called discriminant and stores the value in a variable called result.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
locked card icon
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.