CRT-450 Exam QuestionsBrowse all questions from this exam

CRT-450 Exam - Question 83


A developer has the following class and trigger code.

Which code segment should a developer insert at the XXX to set the baseCost variable to the value of the class variable smokerCharge?

Show Answer
Correct Answer: A

In object-oriented programming, a static variable belongs to the class itself rather than any instance of the class. Thus, it is accessed using the class name rather than an instance of the class. In this case, the variable 'smokerCharge' is declared as static within the 'InsuranceRates' class. Therefore, the correct way to access it is by using 'InsuranceRates.smokerCharge'. This ensures that the variable is accessed directly from the class, not from an instance of the class.

Discussion

8 comments
Sign in to comment
Abhishek36sOption: A
May 20, 2022

A is correct. https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_static.htm A class static variable can’t be accessed through an instance of that class. If class MyClass has a static variable myStaticVariable, and myClassInstance is an instance of MyClass, myClassInstance.myStaticVariable isn’t a legal expression.

NEWBIEGUYOption: D
Jan 22, 2022

the answer is D not A. You are instantiating a reference to the class InsuranceRates as 'rates', which has a static variable named smokerCharge which is accessible outside of that class. As such, it would be rates.smokerCharge

khoce
May 1, 2022

the variable is static and is only accessible as shown in the answer A no instance needed

Therrudy1994Option: A
Apr 14, 2022

Just tried the code in dev console. A is the correct answer.

1vanTTOption: A
May 13, 2023

A is correct. verified it in the dev console. Option D won't even save, you will get the error "Static field cannot be referenced from a non static context: smokerCharge from the type InsuranceRates"

smootaOption: D
Jan 20, 2023

correct answer is D, b/c the InsuranceRates is instatiated by rates.

Nand072
Aug 8, 2023

Wrong, static variable are not accessed via instance. It's only via the class. Basic OOP.

Oldscool8
Mar 20, 2024

Spot on.

ABHI0O07Option: A
Mar 21, 2024

a.BECAUSE SMOKERcHARGE IS A STATIC VARIABLE

SS1121Option: A
Jun 20, 2024

A is correct.

Ankit_SwamiOption: A
Jun 21, 2024

A - Correct A class static variable can’t be accessed through an instance of that class. If class MyClass has a static variable myStaticVariable, and myClassInstance is an instance of MyClass, myClassInstance.myStaticVariable isn’t a legal expression.