Basic CS-Data Structures [TCS Placement]: Sample Questions 13 - 14 of 28

Get unlimited access to the best preparation resource for competitive exams : get questions, notes, tests, video lectures and more- for all subjects of your exam.

Question 13

Data Structures
Edit

Describe in Detail

Essay▾

Convert the expression ( ( (A + B) ⚹ C) - ( (D-E) ^ (F + G) ) ) to equivalent prefix and postfix notations.

Explanation

  • Prefix: ( ( (A + B) ⚹ C) - ( (D-E) ^ (F + G) ) )
    • ( ( (+ AB) ⚹ C) - ( (-DE) ^ (+ FG) ) )
    • ( (⚹+ ABC) - (^ -DE + FG) )
    • -⚹ + ABC ^ -DE + FG
  • Postfix: ( ( (A + B) ⚹ C) - ( (D-E) ^ (F + G) ) )
    • ( ( (AB +) ⚹ C) - ( (DE-) ^ (FG +) ) )
    • ( (AB + C ⚹) - (DE-FG + ^) )
    • AB + C ⚹ DE-FG + ^ -

Question 14

Data Structures
Edit

Describe in Detail

Essay▾

What is lazy initialization?

Explanation

  • Lazy initialization is a process by which objects retrieved via a process or operation are not initialized (or instantiated) before the actual need to use them appears.
  • Prior to . NET 4.0, common example of lazy initialization was the singleton pattern:

Public class DataManager

  1. {
  2.     staticDatamangerdm=null;
  3.     DataManager()
  4.     {
  5.     }
  6.     publicstaticDataManagerGetInstance()
  7.     {
  8.         if(dm== null)
  9.         {
  10.             Dm=newDataManager();
  11.         }
  12.         returndm;
  13.     }
  14. }
  • Here, lazy initialization is used when the DataManger instance is called.
  • If the existing instance is null, then a new instance is created. This example is however not thread safe -if the instance is evaluated in two or more threads, none of the other threads knows about the existing instance (initialized in one of the threads) , so each one of them will create a separate, new instance.
  • In . NET 4.0, there is the Lazy < T > class that facilitates lazy initialization.
  • Applied to previous example, the DataManager class would use lazy initialization:

public class DataManager

  1. {
  2.     staticLazy<DataManager>dm=newLazy<DataManager>(()➾1 newDataManager());
  3.     DataManager()
  4.     {
  5.     }
  6.     publicstaticDataManagerGetInstance()
  7.     {
  8.         returndm.Value;
  9.     }
  10. }
  • There is no DataManager instance in dm until dm. Value is called - only then an active instance of the class is created.
  • Lazy < T > is thread-safe, basically using “first come - first serve” principle - when a thread creates the instance, it will lock that specific instance and the same value will be available across all threads

Developed by: