Which three code segments should you insert in sequence at line 09?

You are developing an application that includes a class named Kiosk. The Kiosk class includes a static
property named Catalog. The Kiosk class is defined by the following code segment. (Line numbers are
included for reference only.)

You have the following requirements:
-Initialize the _catalog field to a Catalog instance.
-Initialize the _catalog field only once.
-Ensure that the application code acquires a lock only when the _catalog object must be instantiated.
You need to meet the requirements.
Which three code segments should you insert in sequence at line 09? (To answer, move the appropriate
code segments from the list of code segments to theanswer area and arrange them in the correct order.)

You are developing an application that includes a class named Kiosk. The Kiosk class includes a static
property named Catalog. The Kiosk class is defined by the following code segment. (Line numbers are
included for reference only.)

You have the following requirements:
-Initialize the _catalog field to a Catalog instance.
-Initialize the _catalog field only once.
-Ensure that the application code acquires a lock only when the _catalog object must be instantiated.
You need to meet the requirements.
Which three code segments should you insert in sequence at line 09? (To answer, move the appropriate
code segments from the list of code segments to theanswer area and arrange them in the correct order.)

Answer:

Explanation:



Leave a Reply 6

Your email address will not be published. Required fields are marked *


Guilherme Morais

Guilherme Morais

This question is the same as 22.

Dhruv

Dhruv

No, it’s not

devprof

devprof

correct answer for thread safty:

get{
lock(_lock)
{
if(_catalog == null)
_catalog = new Catalog();
}
}

devdevprof

devdevprof

should be
get{
if(_catalog == null){
lock(_lock)
{
if(_catalog == null)
_catalog = new Catalog();
}
}
}

no need to lock if the instance is already created.

devdevprof

devdevprof

yeah you should actually, since this is a static object we’re trying to access

Foo

Foo

I may be wrong, but considering the

‘-Ensure that the application code acquires a lock only when the _catalog object must be instantiated.’

clause, I can’t see any other justifiable answer that

if (_catalog == null)
lock (_lock)
if (_catalog == null) _catalog = new Catalog();

, regardless of the correctness of the code.