Standard Template Library (STL)

  Home  Computer Programming  Standard Template Library (STL)


“STL frequently Asked Questions by expert members with experience in Standard Template Library (STL). So get preparation for the STL job interview”



16 Standard Template Library (STL) Questions And Answers

2⟩ How to get the sum of two integers?

#include<stdio.h>

#include<conio.h>

void main()

{

clrscr();

int i,j,sum;

printf("enter two numbers");

scanf("%d%d",&i,&j);

sum=i+j;

printf("sum=%d",sum);

}

 209 views

3⟩ Tell me about c++?

c++ is object oriented programming language.. the main

function value should return.

 200 views

5⟩ What is sorted linked list?

linked list means node which is connected each other with

a line. it means that each node is connected with another

one. Each node of the list hold the referance of the next

node.

if we talk about the sorted linked list , it is also a list

just like another list. but the differce is only that it

hold all the nodes in a sequantial manner either in

accending order decending order

 196 views

6⟩ Explain method overloading?

Method overloading is to overload methods using same class

name by writing different parameters.This is called Method

Overloading.

 191 views

8⟩ Write a program in C/C++ to implement reader- writer problem?

Readers-Writers Problem:

The readers-writers problem is a classic synchronization

problem in which two

distinct classes of threads exist, reader and writer.

Multiple reader threads

can be present in the Database simultaneously. However, the

writer threads must

have exclusive access. That is, no other writer thread, nor

any reader thread,

may be present in the Database while a given writer thread

is present. Note:

the reader/writer thread must call startRead()/startWrite()

to enter the

Database and it must call endRead()/endWrite() to exit the

Database.

class Database extends Object {

private int numReaders = 0;

private int numWriters = 0;

private ConditionVariable OKtoRead = new ConditionVariable

();

private ConditionVariable OKtoWrite = new ConditionVariable

();

public synchronized void startRead() {

while (numWriters > 0)

wait(OKtoRead);

numReaders++;

}

public synchronized void endRead() {

numReaders--;

notify(OKtoWrite);

}

public synchronized void startWrite() {

while (numReaders > 0 || numWriters > 0)

wait(OKtoWrite);

numWriters++;

}

public synchronized void endWrite() {

numWriters--;

notify(OKtoWrite);

notifyAll(OKtoRead);

}

}

class Reader extends Object implements Runnable {

private Monitor m = null;

public Reader(Monitor m) {

this.m = m;

new Thread(this).start();

}

public void run() {

//do something;

m.startRead();

//do some reading…

m.endRead();

// do something else for a long time;

}

}

class Writer extends Object implements Runnable {

private Monitor m = null;

public Writer(Monitor m) {

this.m = m;

new Thread(this).start();

}

public void run() {

//do something;

m.startWrite();

//do some writing…

m.endWrite();

// do something else for a long time;

}

}

- 2 -

wait(ConditionVariable cond) {

put the calling thread on the “wait set” of cond;

release lock;

Thread.currentThread.suspend();

acquire lock;

}

notify(ConditionVariable cond){

choose t from wait set of cond;

t.resume();

}

notifyAll(ConditionVariable cond){

forall t in wait set of cond;

t.resume()

}

For the following scenarios, we assume that only one reader

thread and one writer

thread are running on the processor.

 265 views

9⟩ Write a program in C++ returning starting locations of a substring using pointers?

#include<stdio.h>

#include<iostream.h>

int main()

{

char* mystrstr(char*,char*);

char str1[20];

char str2[10];

cout<<"n Enter two stringst";

cin>>str1>>str2;

cout<<"nstr1 = "<<str1<<" str2 "<<str2 ;

char* c= mystrstr(str1,str2);

if(c!=NULL)

printf("nc = %sn",c);

return 0;

}

char* mystrstr(char* str1, char* str2)

{

char *cp = (char *) str1;

char *s1, *s2;

if ( !*str2 )

return((char *)str1);

while (*cp)

{

s1 = cp;

s2 = (char *) str2;

while ( *s1 && *s2 && !(*s1-*s2) )

{

s1++;

s2++;

}

if (!*s2)

{

printf("n string foundn");

return(cp);

}

cp++;

}

return(NULL);

}

 230 views

15⟩ Why & sign is used in copy constructor?

To avoid local copy of object reference (&) is used in copy

constructor.

Moreover if the & is ommited then copy constructor goes in

infinite loop.

eg. if class name is Sample.

Copy constructor without &

Sample :: Sample (Sample s)

{

//Code goes here

}

and we create object as follows. ;

Sample s;

Sample s1(s);

In this scenario program will go in infinite loop.

 192 views

16⟩ Write a c++ to define a class box with length, breadth and height as data member and input value(), printvalue() and volume() as member functions?

#include<iostream.h>

#include<conio.h>

class BOX

{

private:

int l,b,h;

public:

void input();

void print();

long volume(long,int,int);

};

void BOX::input()

{

cout<<"input values of l,b,&h"<<"n";

cin>>l>>b>>h;

}

void BOX::print()

{

cout<<"volume="<<"n";

}

void BOX::volume()

{

long volume(long l,int b,int h);

{

return(l*b*h);

}

}

void main()

{

set BOX b1;

b1.input();

b1.print();

b1.volume();

return;

}

 180 views