Tuesday, July 31, 2012

How do you flip bits on and off?

Use xor to flip between 1 and 0.  
x = x ^ 1;   // or x ^= 1;
This will change x alternately between 0 and 1.

How do you divide a number by 2 without division?

Just right shift the number by 1.
main()
{
    int x=6;
    printf("%d\n", y>>1); /* Divide by 2 */
    printf("%d\n", y>>2); /* Divide by 4 */
}

Output:
3
1

How do you multiply a number by 2 without multiplication?

Just shift left the number by 1.
main()
{
    int x=6;
    printf("%d\n", x<<1); /* Multiply by 2 */
    printf("%d\n", x<<2); /* Multiply by 4 */
}

Output:
12
24

Monday, July 23, 2012

What is the difference between a Reference and a Pointer?


1. References cannot be null, whereas pointers can;
2. References must be initialized as soon as they are created. On the contrary, it is not mandatory for pointers.
3. A pointer must be manually freed whereas a reference gets automatically dereferenced.
4. You can create the array of Pointer; but you can't create the Array of reference.
5. A pointer can point to many different objects during its lifetime; but a reference can only object during its lifetime.
6. You can declare a constant pointer; but you can't declare a constant reference though it is inherently constant.


What is the difference between '$#' and '$*'?

'$#' variable represents the count of command line parameters whereas '$*' holds the value of command line parameters.

Friday, July 20, 2012

How does compiler differentiate virtual and pure virtual function?

The compiler stores a NULL pointer to the pure virtual function in the V-Table and thus identifies it.

What is the difference between copy constructor and assignment?


A copy constructor is used to initialize a newly declared variable from an existing variable. This makes a deep copy like assignment, but the basic difference are as follows :
  1. There is no need to test to see if it is being initialized from itself.
  2. There is no need to clean up (eg, delete) an existing value (there is none).
  3. A reference to itself is not returned.

Monday, July 16, 2012

What are the modules included in SybPerl?

Sybperl (Sybase extensions to Perl) includes the following four modules:

1. Sybase::DBlib
2. Sybase::CTlib
3. Sybase::BCP
4. Sybase::Sybperl

Sunday, July 15, 2012

How do you check SQL error codes easily in DB2?

You can run the following query :

VALUES SQLERRM(<Error Code>)

For Example, if the error code is "-171" then the query should be :

VALUES SQLERRM(-171)

which shows SQL0171N and its equivalent error description - "THE DATA TYPE, LENGTH, OR VALUE OF ARGUMENT  IS INVALID".

How do you generate and format report in Perl?

#!C:/Perl/bin/perl
format STDOUT_TOP =
-----------------------------------------------------------------------
@||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
"MERA BAZZAR - TERA BAZZAR"
-----------------------------------------------------------------------
@<<<<< @<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<< @<<<<<<<< @>>>>>>> @>>>> @#
CODE,DATE,DESCRIPTION,PRICE,QUANTITY,AMOUNT,Page,$%
-----------------------------------------------------------------------
.
format STDOUT =
@<<<<< @>>>>>>>>> @<<<<<<<<<<<<<<<<<<< @####.## $ @######### @####.## $
$CODE,$DATE,$DESCRIPTION,$PRICE,$QUANTITY,$AMOUNT
.
format STDOUT_TOTAL =
-----------------------------------------------------------------------
            TOTAL = @###.## $
$TOTAL
.
open(INP, "item.dat") || die "Can't read file - $!\n";
while (<INP>)
{
 ($CODE,$DATE,$DESCRIPTION,$PRICE,$QUANTITY) = split(/\t/);
 $AMOUNT=$PRICE * $QUANTITY;
        write;
 $TOTAL += $AMOUNT;
}
close(INP);
$~ = "STDOUT_TOTAL";
write;
# Footer
$^L = '-' x 71 . "\n Copyright, 2012, MERA BAZZAR - TERA BAZZAR \n" . '-' x 71 . "\n";
print ("$^L");

The ITEM.DAT file should contail the following data of columns separated by a TAB :

SOA01        01-01-2012        LUX                   01.35        03
SOA02        03-01-2012        LYRIL                01.15        06
SOA03        02-02-2012        DOVE                01.20        09
SOA04        04-02-2012        HAMMAM        01.10        03
SOA05        12-02-2012        NIRMA              01.36        06
SOA06        03-03-2012        RIN                     01.50        09
SOA07        10-03-2012        SURF                  01.85        03
SOA08        04-04-2012        DETOL               01.90        06

Friday, July 13, 2012

SQL0803N: One or more values in the INSERT statement, UPDATE statement, or foreign key update caused by a DELETE statement are not valid because the primary key, unique constraint or unique index identified by "1" constrains table "STMT_REVISION".

Run the following query to trace the error:


SELECT INDNAME, INDSCHEMA
FROM SYSCAT.INDEXES
WHERE IID = <Index Id>
AND TABSCHEMA = '<Schema Name>'
AND TABNAME = '<Table Name>';


For Example:

SELECT INDNAME, INDSCHEMA
FROM SYSCAT.INDEXES
WHERE IID = 1
AND TABSCHEMA = 'FIG_ADS'
AND TABNAME = 'STMT_REVISION';

What is the difference between do() & prepare() ?


The do() method usually creates, prepares, executes and destroys a statement handle each time. It can be used for non repeated non-SELECT statement (or with drivers that don't support placeholders). For example, if you're doing an UPDATE, INSERT or DELETE there are no data that come back from the database. So you can say:

$dbh->do('DELETE FROM people WHERE age > 65');

do() returns either a true or false value. To be precise, it returns the number of affected rows on success.

On the contrary, the prepare( ) method will almost inevitably result in better performance, particularly on databases which do actually cache the statement. Always replace do( ) with prepare( ) and execute( ) in a loop to attain best performance.

Explain the difference between a static library and a dynamic library.


Static libraries are loaded when the program is compiled and dynamically-linked libraries are loaded in while the program is running. Dynamic libraries reduces the overall footprint of memory as against the static library because linking to static libraries includes the actual code for the library function(s)/procedure(s) with the executable. Dynamic Libraries are kept at specific location and are usually shared among all the processes that use.

Tuesday, July 3, 2012

Monday, July 2, 2012

How could you write colorful text on console in Windows?


#!C:/Perl/bin/perl -w

use Win32::Console;

my $CONSOLE = Win32::Console->new(STD_OUTPUT_HANDLE);

# Get current console colors
my $attr = $CONSOLE->Attr();

# Yellow text on green
$CONSOLE->Attr($FG_YELLOW | $BG_GREEN);
print "This is a test\n";

$CONSOLE->Attr($FG_GREEN | $BG_BLACK);
print "This is a test\n";

# Set console colors back to original
$CONSOLE->Attr($attr);