Pages : 2649780070077928 Price : Rs.
250.
00
Professional
July 2009
CRACKING THE C, C++CRACKING THE C, C++
AND JAVA INTERVIEWAND JAVA INTERVIEW
9780070077928
Pages : 264
Price : Rs.
250.
00
S G Ganesh is currently
working at Siemens,
Bangalore.
He has also
authored the book 60 Tips
on Object Oriented
Programming.
PRIVACY NOTICE:...
Plus
Pages : 2649780070077928 Price : Rs. 250. 00 Professional July 2009 CRACKING THE C, C++CRACKING THE C, C++ AND JAVA INTERVIEWAND JAVA INTERVIEW 9780070077928 Pages : 264 Price : Rs. 250. 00 S G Ganesh is currently working at Siemens, Bangalore. He has also authored the book 60 Tips on Object Oriented Programming. PRIVACY NOTICE: Tata McGraw-Hill Education Pvt. Ltd. values your Privacy. From time to time The through this with other companies whose products or services we feel may be of interest to you. If you would like your name removed from these lists, please mail a written request to Tata McGraw-Hill Education Pvt. Ltd. B-4, Sector-63, Dist. Gautam Budh Nagar, Noida, UP-201 301 or email us at suman_datta@mcgraw-hill. com. For questions about our privacy ractices or to confirm the accuracy of your information, please contact Roystan La Porte, Privacy Official at Tata McGraw-Hill Education Pvt. Ltd. B-4, Sector-63, Dist. Gautam Budh Nagar, Noida, UP-201 301 or call at+9
Moins
Par sgganesh
Document Adobe PDF
Publiée le 2 Sept. 2009
Pages: 1
Lectures: 104
Téléchargements: 0
122 DECEMBER 2007 | LINUX FOR YOU | www.
linuxforu.
com
C M Y K
recently read an interesting article [C++ Report,
Vol.
6, no.
3, ‘How to write buggy programs’ by
Andrew Koenig], which is about writing incorrect
programs.
I’ve taken the following (slightly
modified) piece of code from that article to
illustrate how compiler...
Plus
122 DECEMBER 2007 | LINUX FOR YOU | www. linuxforu. com C M Y K recently read an interesting article [C++ Report, Vol. 6, no. 3, ‘How to write buggy programs’ by Andrew Koenig], which is about writing incorrect programs. I’ve taken the following (slightly modified) piece of code from that article to illustrate how compiler optimisers work: extern void foo(); int main() { if(0) foo(); } The foo function is just declared and is not defined in the program. The if condition always evaluates to ‘false’. Should this program link fine or result in a linker error complaining that the definition of foo is not found? That will depend on the compiler. Some smart compilers see that the statement for if(0) will never get executed, and will therefore not generate a call to foo at all—so the linker won’t complain. However, not all compilers do such smart work in default compilation mode and hence we might get a linker error. (Such compilers might detect the ‘unreachable function call’ at high
Moins
Par sgganesh
Document Adobe PDF
Publiée le 7 Juin 2009
Pages: 1
Lectures: 5
Téléchargements: 0
TABLE OF CONTENTS
Class Design
1.
Provide consistent and intuitive class interface.
5
2.
Provide common properties of classes in a base class.
9
3.
Do not expose implementation details in the public interface of the class.
12
4.
Consider providing helper classes while designing large classes.
14
5.
Keep the data members...
Plus
TABLE OF CONTENTS Class Design 1. Provide consistent and intuitive class interface. 5 2. Provide common properties of classes in a base class. 9 3. Do not expose implementation details in the public interface of the class. 12 4. Consider providing helper classes while designing large classes. 14 5. Keep the data members private. 18 6. Provide lowest possible access to methods. 23 7. Strive for loose coupling between classes. 26 8. Beware of order of initialization problems. 29 9. Write unit tests for classes. 31 10. Avoid low-level code. 34 Object Creation and Handling 11. Avoid calling virtual functions in constructors. 37 12. Consider providing factory methods. 43 13. Make constructor private if there are only static members in the class. 45 14. Avoid creating unnecessary temporary objects. 47 15. Prefer creating immutable objects. 52 16. Consider creating and using null objects. 54 17. Provide special objects with read only access in the class itself
Moins
Par sgganesh
Document Adobe PDF
Publiée le 7 Juin 2009
Pages: 3
Lectures: 12
Téléchargements: 0
12 February 2008 | LINuX For you | www.
openITis.
com
c m y k
Insight
hen can we write highly
efficient code? It is when we
understand how the underlying
machine works and make best
use of that knowledge.
One
well-known way to write highly efficient code
is to write code in assembly.
There are many
disadvantages with this; for...
Plus
12 February 2008 | LINuX For you | www. openITis. com c m y k Insight hen can we write highly efficient code? It is when we understand how the underlying machine works and make best use of that knowledge. One well-known way to write highly efficient code is to write code in assembly. There are many disadvantages with this; for example, we cannot port the programs easily, it is difficult to maintain the code, etc. So, we need to look W for alternatives to write efficient code. We can write in low-level programming languages like C to get write code, whose efficiency is often comparable to the equivalent code written in assembly. For this reason, C is often referred as a ‘high-level assembler’. In this article, we’ll look at various programming constructs from the perspective of efficiency. We’ll consider general machine architecture for illustration; and for specific How many programmers can actually write assembly programs? With the rising popularity of high-level languages (lik
Moins
Par sgganesh
Document Adobe PDF
Publiée le 7 Juin 2009
Pages: 3
Lectures: 15
Téléchargements: 0
www.
linuxforu.
com | LINUX FOR YOU | JUNE 2007
C M Y K
87
I
n my previous column, we saw some of the pitfalls of using
+ and – operators to swap two variables without using a
temporary variable:
*i = *i + *j;
*j = *i - *j;
*i = *i - *j;
Think about the logic in using + and – for such
swapping.
Can we use any other operators for...
Plus
www. linuxforu. com | LINUX FOR YOU | JUNE 2007 C M Y K 87 I n my previous column, we saw some of the pitfalls of using + and – operators to swap two variables without using a temporary variable: *i = *i + *j; *j = *i - *j; *i = *i - *j; Think about the logic in using + and – for such swapping. Can we use any other operators for a similar purpose? Yes, * and / are complementary operators and they can be used for such swapping as well, following the same logic: *i = *i * *j; *j = *i / *j; *i = *i / *j; It works. However, this solution has two problems: if i and j are big, then *i * *j will overflow and by using the division operation you can’t get the original values back; and if *j is 0, the *i / *j expression will result in a divide by zero exception. What about the two complementary bit-wise operators & and | . . . can we use them for swapping by following similar logic? *i = *i & *j; *j = *i | *j; *i = *i | *j; No, it doesn’t work. Unlike + and *, the & operator is not really an
Moins
Par sgganesh
Document Adobe PDF
Publiée le 7 Juin 2009
Pages: 1
Lectures: 2
Téléchargements: 0
Understanding and Using Reflection
Summary: Reflection is a powerful language feature.
Java supports
‘structural-reflection’, which is safe to use.
With reflection, it is
possible to do two basic types of operations on code: inspection and
manipulation.
Reflection enables using ‘signature based polymorphism’
as an...
Plus
Understanding and Using Reflection Summary: Reflection is a powerful language feature. Java supports ‘structural-reflection’, which is safe to use. With reflection, it is possible to do two basic types of operations on code: inspection and manipulation. Reflection enables using ‘signature based polymorphism’ as an alternative to ‘interface based polymorphism’ in Java. Reflection also facilitates creation of flexible and adaptable frameworks and patterns, which is not usually possible with direct code. Though there are many clear advantages in using reflection, like any other language feature, reflection can be misused. This article covers a few specific problems and solutions to illustrate uses and misuses of reflection. In programming, reflection is the ability to dynamically create, use or manipulate classes and objects at runtime. For example, you can create an instance of a class, examine the class to find a specific method from that class and execute that method with the a
Moins
Par sgganesh
Document Adobe PDF
Publiée le 7 Juin 2009
Pages: 8
Lectures: 4
Téléchargements: 0
juLY 2008 | LINuX For You | www.
openITis.
com12
S.
G.
GaneSh
The Joy of
Programming
L
ast month, we introduced a problem in Java: given
two integers i and j, the following statement does
not swap the values of two variables correctly i ^=
(j ^= (i ^= j)).
To understand what is happening, let us analyse the
byte code generated for...
Plus
juLY 2008 | LINuX For You | www. openITis. com12 S. G. GaneSh The Joy of Programming L ast month, we introduced a problem in Java: given two integers i and j, the following statement does not swap the values of two variables correctly i ^= (j ^= (i ^= j)). To understand what is happening, let us analyse the byte code generated for this statement. How do we get the byte codes? By using the javap tool on the generated class file, which will be available in the same directory as javac (in the bin directory). javap is a Java dis-assembler tool that reads the Java class files and dumps the info in a human-readable form. First issue the command, javac Swap. java and then javap -c Swap; you’ll get the detailed output with byte codes. From this output, here is the sequence of byte codes generated for the statement i ^= (j ^= (i ^= j)): 39: iload_1 40: iload_2 41: iload_1 42: iload_2 43: ixor 44: dup 45: istore_1 46: ixor 47: dup 48: istore_2 49: ixor 50: istore_1 Here is a quick intro on
Moins
Par sgganesh
Document Adobe PDF
Publiée le 7 Juin 2009
Pages: 1
Lectures: 5
Téléchargements: 0
12 | March 2009 | LINUX For YoU | www.
openITis.
com
S.
G.
Ganesh
The Joy of Programming | Guest Column
I
nteger overflow happens because computers use fixed
width to represent integers.
So which are the operations
that result in overflow? Bitwise and logical operations
cannot overflow, while cast and arithmetic...
Plus
12 | March 2009 | LINUX For YoU | www. openITis. com S. G. Ganesh The Joy of Programming | Guest Column I nteger overflow happens because computers use fixed width to represent integers. So which are the operations that result in overflow? Bitwise and logical operations cannot overflow, while cast and arithmetic operations can. For example, ++ and += operators can overflow, whereas && or & operators (or even << and >> operators) cannot. Regarding arithmetic operators, it is obvious that operations like addition, subtraction and multiplication can overflow. How about operations like (unary) negation, division and mod (remainder)? For unary negation, -MIN_INT is equal to MIN_INT (and not MAX_INT), so it overflows. Following the same logic, division overflows for the expression (MIN_INT / -1). How about a mod operation? It does not overflow. The only possible overflow case (MIN_ INT % -1) is equal to 0 (verify this yourself—the formula for % operator is “a % b = a - ((a / b) *
Moins
Par sgganesh
Document Adobe PDF
Publiée le 7 Juin 2009
Pages: 1
Lectures: 1
Téléchargements: 0
12 MARCH 2008 | LINUX FoR YoU | www.
openITis.
com
c m y k
Multi-threading
Let’s get the basics of multithreaded programming in Java, and
then write some simple programs.
Overview
An Introduction to
Par sgganesh
Document Adobe PDF
Publiée le 7 Juin 2009
Pages: 4
Lectures: 15
Téléchargements: 0
www.
linuxforu.
com | LINUX FOR YOU | JANUARY 2008
C M Y K
127
1) This program had an assertion failure.
Why?
struct bitfield {
signed int b : 1;
} bit;
int main() {
bit.
b = 1;
assert(bit.
b == 1);
}
2) This function worked fine for many years and
suddenly, for some value of i, the assertion failed.
What was
that value of i for...
Plus
www. linuxforu. com | LINUX FOR YOU | JANUARY 2008 C M Y K 127 1) This program had an assertion failure. Why? struct bitfield { signed int b : 1; } bit; int main() { bit. b = 1; assert(bit. b == 1); } 2) This function worked fine for many years and suddenly, for some value of i, the assertion failed. What was that value of i for which it failed and why? int my_abs(int i) { if (i < 0) { i = -i; } assert(i >= 0); return i; } 3) The following function was written with an intention that the printf should never be executed. But for some value of d, the message got printed! What is that value of ‘d’? void oops(double d) { if(d != d) printf(“Oops! Something went wrong”); } 4) The following program, when run, gave the output: sqrt of 2=1557547102. 000000 instead of: sqrt of 2=1. 414214. What could be the problem? int main() { double x = sqrt(2); printf("sqrt of 2=%lf", x); } 5) This program core-dumped at runtime instead of printing “hi”; can you find out why? int main() { char * msg = ‘hi
Moins
Par sgganesh
Document Adobe PDF
Publiée le 7 Juin 2009
Pages: 1
Lectures: 2
Téléchargements: 0
Unusual Java Bugs
and Fighting them Using FOSS Tools
S G Ganesh
Research Engineer
Siemens (Corporate Technology), Bangalore
Open Source India Week
The TechZone: Developer Track—Bangalore
12-Feb-2008
Par sgganesh
Document Adobe PDF
Publiée le 7 Juin 2009
Pages: 31
Lectures: 2
Téléchargements: 0
An Introduction to C++ Templates
Why Templates?
Generic programming has become a dominant programming paradigm in C++, particularly after
the incorporation of the Standard Template Library (STL) as part of the standard library in 1996.
Templates - the language feature that supports generic programming in C++ - was originally
conceived...
Plus
An Introduction to C++ Templates Why Templates? Generic programming has become a dominant programming paradigm in C++, particularly after the incorporation of the Standard Template Library (STL) as part of the standard library in 1996. Templates - the language feature that supports generic programming in C++ - was originally conceived for supporting ‘parameterized types’ (classes parametrized by type information) in writing container classes. Templates are a compile time mechanism. Because of this, there is no runtime overhead associated with using them. Also, using templates is completely type safe. Templates help to seamlessly integrate all types and thereby let programmers write code for one (generic) type. So, it serves as a mechanism for writing high-level reusable code, which is known as ‘generic programming’. Like the structured, modular and object oriented programming approaches, generic programming is another programming approach (and C++ supports all these four program
Moins
Par sgganesh
Document Adobe PDF
Publiée le 7 Juin 2009
Pages: 5
Lectures: 2
Téléchargements: 0
www.
linuxforu.
com | LINUX FOR YOU | JULY 2007
C M Y K
87
MMMMM
ost of us associate C, or any other programming
language for that matter, with serious coding.
However, programs can also be written just for fun
or aesthetic purposes—like drawing interesting patterns or
pictures.
ASCII art refers to drawing pictures using only...
Plus
www. linuxforu. com | LINUX FOR YOU | JULY 2007 C M Y K 87 MMMMM ost of us associate C, or any other programming language for that matter, with serious coding. However, programs can also be written just for fun or aesthetic purposes—like drawing interesting patterns or pictures. ASCII art refers to drawing pictures using only ASCII code. (ASCII is the most widely used character-encoding scheme for computers, and is based on letters of the English alphabet. ) Much of ASCII art can be generated by computer programs. To give you an introduction to the concept, I’ve written a small program that prints “HELLO” using the ‘@’ character: int main() { // encode the “Hello” string as hex numbers char h[5][5] = { { 0x9, 0xE, 0x8, 0x8, 0xF }, { 0x9, 0x8, 0x8, 0x8, 0x9 }, { 0xF, 0xE, 0x8, 0x8, 0x9 }, { 0x9, 0x8, 0x8, 0x8, 0x9 }, { 0x9, 0xE, 0xE, 0xE, 0xF } }; // mask for reading the characters char m[4] = { 0x08, 0x04, 0x02, 0x01 }; int i = 0, j = 0, k = 0; // for five rows with five characters
Moins
Par sgganesh
Document Adobe PDF
Publiée le 7 Juin 2009
Pages: 1
Lectures: 5
Téléchargements: 0
Believe in the Beauty of Your Dreams
Charles was born in a poor family and lived in streets.
He attended school only for
four years and barely knew to read and write.
The kid s burning desire was to
become a writer, but everything seemed to be against him and the difficulties
seemed insurmountable.
He found a job in a warehouse and...
Plus
Believe in the Beauty of Your Dreams Charles was born in a poor family and lived in streets. He attended school only for four years and barely knew to read and write. The kid s burning desire was to become a writer, but everything seemed to be against him and the difficulties seemed insurmountable. He found a job in a warehouse and was barely able to make his ends meet. Still, the kid dreamt of becoming a writer and wrote in broken English. Fearing to be ridiculed by his slum friends, he posted his first manuscript in night so that he would go unnoticed. The young Charles couldn t resist crying when he saw his first story in print: finally one publisher believed that his words were worthy to print! He was excited with his achievement and wandered around in his slum with tears rolling in his cheeks. Charles Dickens had every reason to give up and make a living out of working as a worker in the factory. Rather his life did get totally transformed, only because he believed in the
Moins
Par sgganesh
Document Adobe PDF
Publiée le 7 Juin 2009
Pages: 2
Lectures: 0
Téléchargements: 0
www.
openITis.
com | LINUX For YoU | AUgUsT 2008 83
S.
G.
GaneSh
The Joy of
Programming
1
.
Which operator in C can result in a ‘divide by
zero’ error other than the / (division) operator?
2.
The conditional operator (? :) is equivalent to ...
Plus
www. openITis. com | LINUX For YoU | AUgUsT 2008 83 S. G. GaneSh The Joy of Programming 1 . Which operator in C can result in a ‘divide by zero’ error other than the / (division) operator? 2. The conditional operator (? :) is equivalent to if-then-else, which is a ternary operator. Why is there no if-then binary (?) operator? 3. Your nephew has scored 453 out of 500 marks in the SSLC exam, and you write a trivial program to check the percentage—what does it print? int main() { int marks = 453, total = 500; float percent = (marks/total)*100; printf(“percentage is = %3. 2f!”, percent); } 4. You want a method that multiplies an integer by 9; will the following work? int mul_by_nine (int x) { return (x << 3 + x); } 5. What is the output of the following program: int main(){ printf(“%d \n”, 1 < 2 < 3); printf(“%d \n”, 3 > 2 > 1); } 6. What is wrong with the following program? #include <assert. h> int main(){ int i = 2; i = -i; assert (i == -2); i = +i; assert (i =
Moins
Par sgganesh
Document Adobe PDF
Publiée le 7 Juin 2009
Pages: 1
Lectures: 2
Téléchargements: 0
www.
linuxforu.
com | LINUX FOR YOU | JANUARY 2007
C M Y K
119
Quines
Write a program that prints the source code of that
program itself (such programs are known as ‘quines’).
The
solution need not be generic; it is enough that the program
written prints itself.
Note:Note:Note:Note:Note: You should not resort to shortcuts such as...
Plus
www. linuxforu. com | LINUX FOR YOU | JANUARY 2007 C M Y K 119 Quines Write a program that prints the source code of that program itself (such programs are known as ‘quines’). The solution need not be generic; it is enough that the program written prints itself. Note:Note:Note:Note:Note: You should not resort to shortcuts such as fileYou should not resort to shortcuts such as fileYou should not resort to shortcuts such as fileYou should not resort to shortcuts such as fileYou should not resort to shortcuts such as file I/O operations to open the source file and print theI/O operations to open the source file and print theI/O operations to open the source file and print theI/O operations to open the source file and print theI/O operations to open the source file and print the contents. contents. contents. contents. contents. Here is a well-known and clever solution: char*f=”char*f=%c%s%c;main(){printf(f,34,f,34,10);}%c”;main() {printf(f,34,f,34,10);} The ASCII values for double-quote
Moins
Par sgganesh
Document Adobe PDF
Publiée le 7 Juin 2009
Pages: 1
Lectures: 12
Téléchargements: 0
JDJ.
SYS-CON.
com10 May 2008
ny large Java source base can have insidious and
subtle bugs.
Every experienced Java programmer
knows that finding and fixing these bugs can be
difficult and costly.
Fortunately, there are a large
number of free open source Java tools available that can
be used to find and fix defects early in the...
Plus
JDJ. SYS-CON. com10 May 2008 ny large Java source base can have insidious and subtle bugs. Every experienced Java programmer knows that finding and fixing these bugs can be difficult and costly. Fortunately, there are a large number of free open source Java tools available that can be used to find and fix defects early in the development lifecycle. In this article, we’ll look at a few examples of specific uncommon or unusual defects that can happen in code and see how different Java static analysis tools detect them. Testing As software gets more complex and ubiquitous, it becomes more difficult to ensure high-quality code. One common method of finding bugs is testing. But testing can’t cover all paths and possibilities or enforce good programming practices. Expert knowledge in the form of manual code review by peers is one of the best ways to ensure good code quality. Code review is often used as a mandatory process step for improving the code and for finding the problems early in t
Moins
Par sgganesh
Document Adobe PDF
Publiée le 7 Juin 2009
Pages: 4
Lectures: 6
Téléchargements: 0
c m y k
127www.
openITis.
com | LINUX For YoU | FebrUarY 2008
In the following programs, assume that necessary header
files are included.
Q1.
Will this program result in an assertion failure?
int main() {
assert(sizeof(void *) == sizeof(int *));
assert(sizeof(int *) == sizeof(int **));
}
Q2.
What will this program print?
int main()...
Plus
c m y k 127www. openITis. com | LINUX For YoU | FebrUarY 2008 In the following programs, assume that necessary header files are included. Q1. Will this program result in an assertion failure? int main() { assert(sizeof(void *) == sizeof(int *)); assert(sizeof(int *) == sizeof(int **)); } Q2. What will this program print? int main() { int iarr[10]; int *i = &iarr[2], *j = &iarr[5]; int *k = i + j; int diff = j – i; printf(“%d”, diff); } Q3. Will this program work? int main() { int i = “C is often unpredictable!”; printf(i); } Q4. What does the following program print? int main() { char string[10]; printf(strncpy(string ,”Joy of C”,3)[3] = ‘\0’); } Q5. What does this following program print? int main() { // assume that address of i is 0x1234ABCD int i = 10; int * ip = &i; int **ipp = &&i; printf(“%x, %x, %x”, &i, ip, *ip); } Well, they don’t seem too difficult, do they? It is too soon S. G. GaneSh Pointers are the forte of C, are the most difficult to master, and programming with
Moins
Par sgganesh
Document Adobe PDF
Publiée le 7 Juin 2009
Pages: 3
Lectures: 0
Téléchargements: 0