Subscribe:Posts Comments | Read Articles on Various Topics - Pro Articles

You Are Here: Home » C and C++, Technical Interview Questions » C and C++ Employment Test Questions

C and C++ Employment test questions for freshers, experienced graduates and diploma holders. See more c and c++ resources for technical interview and written test paper preparation

  1. How to run DIR command programmatically?
    Ans: We can use the system( ) function to execute the DIR command along with its options. Following program shows how this can be achieved:
    // mydir.c
    main ( int argc, char *argv[ ] )
    {
    char str[30] ;
    if ( argc < 2 )
    exit ( 0 ) ;
    sprintf ( str, “dir %s %s”, argv[1], argv[2] ) ;
    system ( str ) ;
    }
    If we run the executable file of this program at command prompt passing the command line arguments as follows:
    > mydir abc.c /s
    This will search the file ‘abc.c’ in the current directory.

  2. Suppose I have a structure having fields name, age, salary and have passed address of age to a function fun( ). How I can access the other member of the structure using the address of age?
    Ans:
    struct emp
    {
    char name[20] ;
    int age ;
    float salary ;
    } ;
    main( )
    {
    struct emp e ;
    printf ( “\nEnter name: ” ) ;
    scanf ( “%s”, e.name ) ;
    printf ( “\nEnter age: ” ) ;
    scanf ( “%d”, &e.age ) ;
    printf ( “\nEnter salary: ” ) ;
    scanf ( “%f”, &e.salary ) ;
    fun ( &e.age ) ;
    }
    fun ( int *p )
    {
    struct emp *q ;
    int offset ;
    offset = ( char * ) ( & ( ( struct emp * ) 0 ) -> age ) – ( char * ) ( (
    struct emp* ) 0 ) ;
    q = ( struct emp * ) ( ( char * ) p – offset ) ;
    printf ( “\nname: %s”, q -> name ) ;
    printf ( “\nage: %d”, q -> age ) ;
    printf ( “\nsalary: %f”, q -> salary ) ;
    }

  3. How to restrict the program’s output to a specific screen region?
    Ans: A C function window( ) can be used to restrict the screen output to a specific region. The window( ) function defines a text-mode window. The parameters passed to this function defines the upper-left and lower-right corner of the region within which you want the output. In the following program, the string ‘Hello!’ gets printed within the specified region. To print the string we must use cprintf( ) function which prints directly on the text-mode window.
    #include
    main( )
    {
    int i, j ;
    window ( 20, 8, 60, 17 ) ;
    for ( i = 0 ; i < 8 ; i++ )
    for ( j = 0 ; j < 10 ; j++ )
    cprintf ( “Hello!” ) ;
    }

  4. Sometimes you need to prompt the user for a password. When the user types in the password, the characters the user enters should not appear on the screen. A standard library function getpass( ) can be used to perform such function. Maximum number of characters that can be entered as password is 8.
    main( )
    {
    char *pwd ;
    pwd = getpass ( “Enter Password” ) ;
    if ( strcmp ( pwd, “orgcity” ) )
    printf ( “\nPassword %s is incorrect”, pwd ) ;
    else
    printf ( “\nCorrect Password” ) ;
    }

  5. How to obtain the current drive through C ?
    Ans: We can use the function _getdrive( ) to obtain the current drive. The _getdrive( ) function uses DOS function 0X19 to get the current drive number
    #include
    main( )
    {
    int disk ;
    disk = _getdrive( ) + ‘A’ – 1 ;
    printf ( “The current drive is: %c\n”, disk ) ;

Leave a Reply

© Copyright 2010 Latestexams. All Rights Reserved | Privacy Policy | Terms & Conditions