| preferred AP College board partner for AP classes
AP Computer Science A/Unit 2: Using Objects
Start Practice TestPractice Test
About Exam
medium Solved by 15 students
Java Overloaded Constructor Calls
< Prev
Next >

Consider the following Temperature class, which contains overloaded constructors.

public class Temperature {
    private double celsius;
    
    public Temperature(double c) {
        celsius = c;
    }
    
    public Temperature(int f) {
        celsius = (f - 32) * 5.0 / 9.0;
    }
    
    public Temperature(double f, boolean isFahrenheit) {
        if (isFahrenheit) {
            celsius = (f - 32) * 5.0 / 9.0;
        } else {
            celsius = f;
        }
    }
}

The following code segment attempts to create several Temperature objects.

Temperature t1 = new Temperature(25.0);
Temperature t2 = new Temperature(77);
Temperature t3 = new Temperature(98.6, true);
Temperature t4 = new Temperature(20, false);

Which statement best describes the result of compiling this code segment?

A

Only t1 and t3 will compile successfully

B

t1, t2, and t3 will compile successfully, but t4 will cause a compilation error

C

All four constructor calls will compile and execute successfully

D

t2 and t4 will both cause compilation errors due to type mismatches

Hint
Did You Know?
Explain Why
Explain All Answers
Check Answer
Show Correct Answer
Report Question

Question Leaderboard

Rank
User
Correct Count
Attempt Count
Time
Score
#1zmirzaucl11 0m 00s 100
#2kaisuki12 0m 00s 90
#3liuwilliam07241012 1m 07s 23
#4patrickmeijer00913 1m 10s 10
#5psak1211 1m 42s -2
#6sailajavadlamani3302 0m 00s -20
#7jeonsaw172311 2m 01s -21
#8richa.tuli12 2m 17s -47
#9y.seong202711 2m 34s -54
#10ananyetyagi01 1m 55s -125
Items per page:
10
1 – 10 of 13
No comments yet. Be the first to comment!

AI Tutor

How can I help?

APFIVE © 2020.
Email: [email protected]|Privacy Policy