QUESTION 1 The following code involves duplicate variable declarations and won\'
ID: 3598279 • Letter: Q
Question
QUESTION 1
The following code involves duplicate variable declarations and won't compile.
public void t1(int i) {
for (int i=0; i<5; i++) {
//empty body
}
}
a. True
b. False
QUESTION 2
The following code involves duplicate variable declarations and won't compile.
public void t2(int i) {
for (int k =0; k<=5; k++) {
// empty
}
int k=10;
}
a. True
b. False
QUESTION 3
The BankAccount class is implemented as the following:
public class BankAccount{
private double balance;
public BankAccount(balance) {
setBalance(balance);
}
public getBalance() {
return balance;
}
public setBalance(double balance) {
this.balance=balance;
}
}
When you run the following program, what will be the output?
public class Test{
public static void main(String[] args) {
BankAccount acct = new BankAccount(75);
h(acct);
System.out.println(acct.getBalance());
}
public static void h(BankAccount a) {
a.setBalance(100);
}
}
a.
75.0
b.
0.0
c.
100.0
d.
null
QUESTION 4
Study the following program:
public class T3 {
public static void main(String[] args) {
int x = 10;
h(x);
System.out.println(x);
}
public static void h (int x) {
x += x++;
}
}
When the program runs, the value it outputs is ___ (give an integer value).
QUESTION 5
Study the following program:
public class T4 {
public static void main{String[] args) {
h(1);
}
public static void h() {
System.out.println("1");
}
public static void h(int n) {
n /=2;
System.out.println(10);
}
public static void h(String n) {
System.out.println(100);
}
}
When the program runs, what will be the output?
a.
1
b.
5
c.
10
d.
100
QUESTION 6
Study the following partially finished code:
public class Student{
private String name, major;
public Student(String name) {
// call 2-argument constructor with "MIS" as major
}
public Student(String name, String major) {
this.name=name;
this.major=major;
}
//getters and setters;
}
Which of the following is correct implementation of the 1-argument constructor according the instruction?
a.
this(name, "MIS");
b.
this.name=name;
this(name, "MIS");
c.
Student(name, "MIS");
QUESTION 7
Suppose the Student class in previous question has been correctly implemented. The main method of a program contains the following code:
Student s = new Student("Alice");
System.out.println(s.getMajor());
What is the output when the program runs?
a.
null
b.
CS
c.
Nothing is in the output
d.
MIS
a.
75.0
b.
0.0
c.
100.0
d.
null
Explanation / Answer
QUESTION 1
Answer: a.True
QUESTION 2
Answer:b.False
QUESTION 3
Answer: c. 100.0
QUESTION 4
Answer: 10
QUESTION 5
Answer: c. 10
QUESTION 6
Answer: a. this(name, "MIS");
QUESTION 7
Answer: d. MIS
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.