Java Midterm 2 – Flashcards

Unlock all answers in this set

Unlock answers
question
Which of the following declares an integer instance variable? a. private dimes int; b. private int dimes; c. private Int dimes; d. private Integer dimes;
answer
b. private int dimes;
question
Given: Scanner keyboard = new Scanner(System.in); int answer; which statement will take the user input and set the variable answer to that value? a. answer = keyboard; b. answer = (int)keyboard; c. answer = keyboard.nextInt(); d. answer = Scanner.keyboard.nextInt();
answer
c. answer = keyboard.nextInt();
question
In the statement: new BankAccount(1585940, 200, "John","Smith"); the values in the parentheses are called... a. definers b. construction parameters c. definition values d. method return type
answer
b. construction parameters
question
Local variables die when.. a. the object is no longer in use b. the method exists c. you explicitly delete them d. the program ends
answer
b. the method exists
question
A method that performs some action other than returning a value is called a ____ method. a. null b. void c. public d. private
answer
b. void
question
A variable whose meaning is confined to an object of a class is called: a. instance variable b. local variable c. global variable
answer
a. instance variable
question
A variable whose meaning is confined to a method definition is called ____. a . instance variable b. local variable c. global variable
answer
b. local variable
question
The name of a method and the list of _____ types in the heading of the method definition is called the method signature. a. parameter b. argument c. return d. primitive
answer
a. parameter
question
True or False. Method overloading is when two or more methods of the same class have the same name but differ in number or types of parameters.
answer
True
question
True or False. A value of a class type is called an object or an instance of the class.
answer
True
question
Which of the following NOT correct? a. String greeting="hello"'; System.out.println(greeting); b. String greeting="hello";int n=greeting.length(); c. String greeting="hello"; System.out.println(greeting.length()); d. String greeting="hello"; int k=greeting.length;
answer
d. String greeting="hello"; int k=greeting.length;
question
What is the return type of the following method: public void getBalance(String name, int accountNr) a. The method does not return a value b. String c. int d. public
answer
a. The method does not return a value
question
Suppose that you have class BankAccount: public class BankAccount{ private double balance; public void withdraw(double amount){balance=balance-amount;} public void deposit(double amount){balance=balance+amount;} and main class containing the following code: BankAccount account=new BankAccount(); deposit(5000); withdraw(1000); deposit(5000); withdraw(2000); what is the balance of the account? a. 7000 b. 1100 c. The syntax is wrong d. 1000
answer
c. The syntax is wrong
question
Suppose that BankAccount class looks like: public class BankAccount{private double balance; public void withdraw(double amount){balance=balance-amount;} public void monthlyFee(){ //code} } What's the correct way of implementing monthlyFee method? a. this.withdraw(10); b. withdraw(10); c. none of the above d. a and b
answer
d. a and b
question
Examine the code below: int d; Random generator=new Random(); int d=generator.nextInt(6); What is the range of d? a. [0,1,2,3,4,5] b. [0,1,2,3,4,5,6] c. [1,2,3,4,5] d. [1,2,3,4,5,6]
answer
a. [0,1,2,3,4,5]
question
The method toString() is a public member of the class ___. a. Object b. String c. Writer d. Output
answer
a. Object
question
Which of the following is a valid statement? a. String name("Doe"); b. String("Doe"); c. String name = "Doe"; d. name = new String;
answer
c. String name = "Doe";
question
Given the method heading public static String exampleMethod(int n, char ch) What is the return type of the value returned? a. int b. char c. String d. Nothing will be returned
answer
c. String
question
True or False. The return statement must be the last line of the method.
answer
False
question
True or False. The class Object is directly or indirectly the superclass of every class in Java.
answer
True
question
True or False. The new operator can be used to create an object.
answer
True
question
A static method is one that can be used with a _____. a. instance variable b. local variable c. global variable d. the class name as a calling object
answer
d. the class name as a calling object
question
Only ____ copy/copies of a static variable are available to objects of a class. a. one b. two c. three d. none of the above
answer
a. one
question
All of these are methods of Java's Math class except: a. pow b. min c. random d. toString
answer
d. toString
question
The Math method that returns the nearest whole number that is greater than or equal to its argument is: a. round b. ceil c. floor d. none of the above
answer
b. ceil
question
All of the following are wrapper classes except: a. String b. Integer c. Character d. Double
answer
a. String
question
The method trim of the String class trims off: a. Leading white space b. Trailing white space c. Leading and trailing white space d. Blanks
answer
c. Leading and trailing white space
question
When you use the assignment operator with variables of a class type, you are assigning a: a. value b. primitive type c. local variable d. reference
answer
d. reference
question
True or False. You may use methods of the Math class without an import statement.
answer
True
question
True or False. Wrapper classes are provided for all primitive Java types except Boolean.
answer
False
Get an explanation on any task
Get unstuck with the help of our AI assistant in seconds
New