Tuesday, May 29, 2012

perl interview questions

  • Explain the difference between 'my' and 'local' variable scope declarations?

Both of them are used to declare local variables.
The variables declared with 'my' can live only within the block and cannot gets its visibility inherited fucntions called within that block, but one defined as 'local' canlive within the block and have its visibility in the functions called within that block.




'my' creates a new variable, 'local' temporarily amends the value of a variable

There is a subtle difference.

In the example below, $::a refers to $a in the 'global' namespace.

$a = 3.14159;
{
local $a = 3;
print "In block, $a = $a ";
print "In block, $::a = $::a ";
}
print "Outside block, $a = $a ";
print "Outside block, $::a = $::a ";

# This outputs
In block, $a = 3
In block, $::a = 3
Outside block, $a = 3.14159
Outside block, $::a = 3.14159
[download]
ie, 'local' temporarily changes the value of the variable, but only within the scope it exists in.

so how does that differ from 'my'? 'my' creates a variable that does not appear in the symbol table, and does not exist outside of the scope that it appears in. So using similar code:

$a = 3.14159;
{
my $a = 3;
print "In block, $a = $a ";
print "In block, $::a = $::a ";
}
print "Outside block, $a = $a ";
print "Outside block, $::a = $::a ";

# This outputs
In block, $a = 3
In block, $::a = 3.14159
Outside block, $a = 3.14159
Outside block, $::a = 3.14159
[download]
ie, 'my' has no effect on the global $a, even inside the block.

http://www.coolinterview.com/interview/6205/






  • what is meant by a 'pack' in perl?

Pack Converts a list into a binary representation. Takes an array or list of values and packs it into a binary structure

http://discuss.itacumens.com/index.php?topic=37847.0




  • What are scalar data and scalar variables?

Scalar means a single thing, like a number or string

http://www.toppersarena.com/index.php?option=com_content&view=article&id=217:perl-interview-questions-and-answers&catid=36:blog-section&Itemid=49



  • What is a hash?

conventions remain the same - any combination of letters, numbers and the underscore characte
%special_events
A hash is also called an associative array, or an array where each value is associated with a corresponding key
%contactInfo = ('name', 'Bob', 'phone', '111-111-1111');
http://www.techinterviews.com/perl-interview-questions-and-answers
http://www.onesmartclick.com/interviews/perl-programming-interview-questions-answers.html




What does the command ‘use strict’ do and why should you use it?

#!/usr/local/bin/perl -w
use strict;

This will help you (really, it will force you) to write better, cleaner code.
Adding the -w switch to the perl interpreter will cause it to spit out warnings on uninitialized variables
'use strict' turns on the 'strict' pragma which will make you declare your variables with the my keyword
'use strict' also tosses up errors if your code contains any barewords that can't be interpreted in their current context.

http://perl.about.com/b/2006/09/21/why-you-should-use-strict.htm





  • "Use strict" is one of the pragma used for checking the variable scope and syntax


Eg: You can write a program like this:

$x=100
print $x

But if you use strict pragma, then you have to be very
specific while using variables

use strict;
my $x=100; ##look at the usage of scope like my,local,our
print $x;

http://www.allinterview.com/showanswers/16873.html






  • Explain the difference between use and require.


The use statement works at compile time, require at run time. So if you have a module that does a lot in a begin block and you don't really need it in all cases, then it's clever to "require" it there where you want to use it but don't "use" it. So you don't have to wait for all that initializations of that module in case you don't need it.

The differences are many and often subtle:
use only expects a bareword, require can take a bareword or an expression
use is evaluated at compile-time, require at run-time
use implicitly calls the import method of the module being loaded, require does not
use excepts arguments in addition to the bareword (to be passed to import), require does not
use does not behave like a function (i.e can't be called with parens, can't be used in an expression, etc), whereas require does
So they behave differently but achieve the same goal. Then there's a list of cultural differences in addition to the technical differences, but they're not so hard and fast.

http://www.perlmonks.org/?node_id=412860




  • What purpose does each of the following serve: -w, strict, - T ?

-w enables the warnings mode in perl-T is enables the Taint mode it performs some checks howyour program is using the data passed to itTurn on strict mode to make Perl check for common mistakes.

http://www.scribd.com/doc/1184228/Perl-Interview


-w:Enables warnings.Turning on warnings will make Perl yelp and complain at a huge variety of things that are almost always sources of bugs in your programs.

-t:Perl offers a mechanism called taint that marks any variable that the user can possibly control as being insecure. This includes user input, file input and environment variables.

Strict: It makes you declare all your variables (``strict vars''), and it makes it harder for Perl to mistake your intentions when you are using subs (``strict subs'').


  • Difference between for & foreach ?
For and foreach both are for looping the code but if you want to use array in place of scalar for loop, you have to use foreach keyword

  • What is a subroutine?
subroutine is a reusable piece of code.


  • Why we use Perl?
1.Perl is a powerful free interpreter.
2.Perl is portable, flexible and easy to learn.
-For shell scripting
-For CGI 
-Tons of scripts are available.
-Easy development
-Enormous big support script archive like CPAN
-No one starts to write a Perl scripts from scratch, you choose one from an archive and modify that.
-It is a "mature" scripting language.
-You may find Perl interpreter on every mission critical environment
-Easy to learn


  • what's hash?
Hash in basically used to comment the script line.
A hash is and unordered set of key/value pairs that you access using strings (keys) as subscripts, to look up the scalar value corresponding to a given key.


  • Name all the prefix dereferencer in perl?
The symbol that starts all scalar variables is called a prefix dereferencer. The different types of dereferencer are.
(i) $-Scalar variables
(ii) %-Hash variables
(iii) @-arrays
(iv) &-subroutines
(v) Type globs-*myvar stands for @myvar, %myvar.




  • What's the difference between grep and map in Perl?

grep returns those elements of the original list that match the expression, while map returns the result of the expression applied to each element of the original list.
http://perlknowledge.blogspot.com/2010/12/perl-basic-interview-question-and.html




how to perl on linux



@variables



A variable is defined by the ($) symbol (scalar)
the (@) symbol (arrays)
the (%) symbol (hashes)


Scalar variables are simple variables containing only one element--a string, a number, or a reference

Strings may contain any symbol, letter, or number.
Numbers may contain exponents, integers, or decimal values.
The bottom line here with scalar variables is that they contain only one single piece of data


Arrays contain a list of scalar data (single elements).

Hashes are complex lists with both a key and a value part for each element of the list.

Reference:
http://www.tizag.com/perlT/perlvariables.php



$a=1;
($a,$b)=(7,8);

$nummer1 =3;
$string1="estamboel";

print "$string1 $nummer1 \n";

print "$string1 + $nummer1 \n";

Reference:

http://www.youtube.com/watch?v=y25VxNiGc2M&feature=autoplay&list=PLE7511681ABEA8635&playnext=2





@syntax

File names, variables, and arrays are all case sensitive

'#' sign. Any words, spaces, or marks after a pound symbol will be ignored

use the backslash (\) character to escape any type of character that might interfere with our code



@strings

There is no limit to the size of the string, any amount of characters
string can be used with single or double quotations

@numbers

Numbers exist as real numbers, float, integers, exponents, octal, and hexidecimal numbers

@arrays

sequential number arrays
Rather than typing out each element when counting to 100 for example
@100 = (1 .. 100);

finding the length of an array
set the array to a scalar variable, then just print the new variable
@nums = (1 .. 20);
$nums = @nums;


adding and removing elements

push() - adds an element to the end of an array.
unshift() - adds an element to the beginning of an array.
pop() - removes the last element of an array.
shift() - removes the first element of an array.

Function Definition
push(@array, Element) Adds to the end of an array
pop(@array) Removes the last element of the array
unshift(@array, Element) Adds to the beginning of an array
shift(@array) Removes the first element of an array
delete $array[index] Removes an element by index number






how to run simple perl code on linux

Platform
Red Hat Enterprise Linux 5 (RHEL 5)

@Locate where the perl is installed.you can try where and which commands


[yeniceri@localhost ~]$ whereis perl
perl: /usr/bin/perl     /usr/share/man/man1/perl.1.gz


[yeniceri@localhost ~]$ which perl
/usr/bin/perl


@create a new file and type the following code

[yeniceri@localhost ~]$ vi test.pl 

#!/usr/bin/perl
print "hello world \n";

@execute test.pl

[yeniceri@localhost ~]$ perl test.pl 
hello world 

there's another way but you should grant access first


[yeniceri@localhost ~]$ ls -l test.pl
-rw-rw-r-- 1 yeniceri yeniceri 71 May 29 04:31 test.pl
[yeniceri@localhost ~]$ chmod u+x test.pl
[yeniceri@localhost ~]$ ls -l test.pl
-rwxrw-r-- 1 yeniceri yeniceri 71 May 29 04:31 test.pl
[yeniceri@localhost ~]$ ./test.pl
hello world


Reference;
http://www.tizag.com/perlT/perlvariables.php




Spring AOP



aspects get executed just before related objects run
aspects look like the way triggers work in the database
according to spring configuration files before a method is executed spring configuration file is checked.

aspect oriented programming is different because if there are three objects and these three objects share a common class to use a method you have to extend or make
references as what happens normally in object oriented programming.however in aspect programming you don't need to do these.you create special
classes called aspects and a configuration file.whenever few objects invoke a method before objects are executed aspect configuration file checks on them and then run the aspect class which is also referenced in aspect configuration file

Reference:
http://www.youtube.com/watch?v=QdyLsX0nG30&list=PLE37064DE302862F8&index=1&feature=plpp_video

Wednesday, May 9, 2012

why JPA?




Why JPA?

A fundamental question for many Java developers is "Why JPA?
 Why do I need to know how to use this API when object-relational mapping tools like Hibernate and Toplink are already available?"

The answer is that JPA is not a new technology; rather, it has collected the best ideas from existing persistence technologies like Hibernate, TopLink, and JDO.

The result is a standardized specification that helps you build a persistence layer that is independent of any particular persistence provider.

http://www.javaworld.com/javaworld/jw-01-2008/jw-01-jpa1.html





The Java Persistence API - A Simpler Programming Model for Entity Persistence

A major enhancement in EJB technology is the addition of the new Java Persistence API, which simplifies the entity persistence model and adds capabilities that were not in EJB 2.1 technology.

The Java Persistence API deals with the way relational data is mapped to Java objects ("persistent entities"), the way that these objects are stored in a relational database so that they can be accessed at a later time, and the continued existence of an entity's state even after the application that uses it ends.

In addition to simplifying the entity persistence model, the Java Persistence API standardizes object-relational mapping


The Java Persistence API draws on ideas from leading persistence frameworks and APIs such as Hibernate, Oracle TopLink, and Java Data Objects (JDO), and well as on the  earlier EJB container-managed persistence


The Java Persistence API simplifies the programming model for entity persistence and adds capabilities that were not in EJB 2.1.

Here's a quick list of its simplifications and additions:

Provides cleaner, easier, standardized object-relational mapping
Eliminates the need for lookup code
Adds support for inheritance, polymorphism, and polymorphic queries.
Provides a Java Persistence query language -- an enhanced EJB QL


http://www.oracle.com/technetwork/articles/javaee/jpa-137156.html

Tuesday, May 8, 2012

What is software architecture of EJB?



Session and Entity EJBs consist of 4 and 5 parts respetively:

1. A remote interface (a client interacts with it),

2. A home interface (used for creating objects and for declaring business methods),

3. A bean object (an object, which actually performs business logic and EJB-specific operations).

4. A deployment descriptor (an XML file containing all information required for maintaining the EJB) or a set of deployment descriptors (if you are using some container-specific features).

5.A Primary Key class - is only Entity bean specific.

What makes J2EE suitable for distributed multitiered applications?


What makes J2EE suitable for distributed multitiered applications?

The J2EE platform uses a multitiered distributed application model. Application logic is divided into components according to function, and the various application

components that make up a J2EE application are installed on different machines depending on the tier in the multitiered J2EE environment to which the application component belongs


Client-tier components run on the client machine.
Web-tier components run on the J2EE server.
Business-tier components run on the J2EE server.
Enterprise information system (EIS)-tier software runs on the EIS server.

What are the components of J2EE application?


What are the components of J2EE application?

Application clients and applets are client components.
Java Servlet and JavaServer Pages technology components are web components.
Enterprise JavaBeans components (enterprise beans) are business components.

Are JavaBeans J2EE components?


Are JavaBeans J2EE components?
No.JavaBeans components written for the J2EE platform have instance variables and get and set methods for accessing the data in the instance variables.

Differences between EJB 3.0 and EJB 2.1


Differences between EJB 3.0 and EJB 2.1

EJB2.0 has Deployment descriptors but in EJB3.0 has no Deployment Descriptors.
EJB2.0 we have to write Home and Remote Interfaces But in EJB3.0 No need to write Home and remote interfaces

Compared to EJB 2.1, EJB 3.0 simplifies the process of creating Enterprise JavaBean applications.

The underlying concept of the EJB 3.0 specification centers on a plain old Java™ object (POJO) programming model that uses Java annotations to capture information that deployment descriptors used to contain

Deployment descriptors are now optional in most cases.

session beans also no longer require EJB-specific component interfaces either.

EJB 3.0 has introduced a lightweight entity bean persistence mechanism through the Java Persistence API. These entities are POJO based and can then be run outside of an EJB container and do not require any interfaces or EJB code in them

What is the EAR file?

An EAR file is a standard JAR file with an .ear extension, named from Enterprise ARchive file. A J2EE application with all of its modules is delivered in EAR file.

Monday, May 7, 2012

auto-boxing, boxing, unboxing



  • Lesson3 Boxing & Unboxing

http://www.youtube.com/watch?v=ujbeM5MD84M&feature=autoplay&list=ULr_jhKrpDD5c&playnext=1



  • java 09 - Wrappers and Boxing

http://www.youtube.com/watch?v=KUdvCjLqP_0
wrapper class wraps primitive type into an object





  • Java Tutorial 7.1 Autoboxing Part 1/3

http://www.youtube.com/watch?v=JPIN1vZBM8M&feature=relmfu


  • Java Tutorial 7.2 Autoboxing Part 2/3

http://www.youtube.com/watch?v=uGLoHyjURtc&feature=relmfu


  • java Tutorial 7.3 Autoboxing Part 3/3

http://www.youtube.com/watch?v=jqKn_dCBU20&list=UU77mSrM47r2NgFoxGSl4DXw&index=154&feature=plpp_vide



  • Autoboxing

Autoboxing and unboxing is introduced in Java 1.5 to automatically convert primitive type into boxed primitive( Object or Wrapper class).
before Java 1.5 then you are familiar with the issues like you can not directly put primitives into Collections, instead you first need to convert them into Object only then only you can put them into Collections. Wrapper class like Integer
With the introduction of autoboxing and unboxing in Java this primitive to object conversion happens automatically by Java compiler which makes code more readable


it becomes even more important because they are automatic and can create subtle bugs if you are not sure when autoboxing  in Java code occurs and when unboxing happens.

When Java automatically converts a primitive type like int into corresponding wrapper class object e.g. Integer than its called autoboxing  because primitive is boxed into wrapper class while in opposite case is called unboxing, where an Integer object is converted into primitive int.


All primitive types e.g. byte, short, char, int, long, float, double and boolean has corresponding wrapper class e.g. Byte, Short, Integer, Character etc and participate in autoboxing and unboxing.


During autoboxing boolean is converted to Boolean, byte to Byte, char converted to Character, float changes to Float, int goes to Integer, long goes to Long and short converts to Short, while in unboxing opposite happens like Float to float.

for example In method invocation where an object argument is expected,  if you pass primitive, Java automatically converts primitive into equal value Object.

Autoboxing mainly occur in two places one is during assignment and other is during method invocation


One of the danger of autoboxing is throw away object which gets created if autoboxing occurs in a loop. Here is an example of how unnecessary object can slow down your application


Integer sum = 0;
 for(int i=1000; i<5000 i="i" p="p">   sum+=i;
 }

In this code sum+=i will expand as sum = sum + i and since + operator is not applicable to Integer object it will trigger unboxing of sum Integer object and then autoboxing of result which will be stored in sum which is Integer as shown below :

sum = sum.intValue() + i;
Integer sum = new Integer(result);    
   
here since sum is Integer, it will create around 4000 unnecessary Intger object which are just throw away and if this happens on large scale has It potential to slow down system with frequent GC for arithmetic calculation always prefer primitive over boxed primitive and look for unintentional autoboxing in Java

autoboxing is error prone e.g. equality operator "==". Since equality operator can be applied on both primitive and Objects it leads to confusion and can cause subtle issues

When you compare two object using "==" operator it compares object's identity and not value and also no auto boxing occur.
its not best practice to use  equality operator to compare Objects, use equals method instead

if we compare one primitive with another object than unboxing of object is occur which could throw NullPointerException if object is null e.g.

Since autoboxing creates unnecessary object and if that goes beyond a limit usually outside the range of cached value it can potentially slow your program by frequently causing garbage collection.



http://javarevisited.blogspot.com/2012/07/auto-boxing-and-unboxing-in-java-be.html#ixzz22r1HOohK





what is transaction?



  • Lesson6 Transactions

http://www.youtube.com/watch?v=r_jhKrpDD5c&feature=channel&list=UL



  • transaction

In computer programming, a transaction usually means a sequence of information exchange and related work (such as database updating) that is treated as a unit for the purposes of satisfying a request and for ensuring database integrity.

A typical transaction is a catalog merchandise order phoned in by a customer and entered into a computer by a customer representative. The order transaction involves checking an inventory database, confirming that the item is available, placing the order, and confirming that the order has been placed and the expected time of shipment. If we view this as a single transaction, then all of the steps must be completed before the transaction is successful and the database is actually changed to reflect the new order. If something happens before the transaction is successfully completed, any changes to the database must be kept track of so that they can be undone.
http://searchcio.techtarget.com/definition/transaction



  • What Is a Transaction Processing System?

Transaction processing system is a program or software that processes all transactions that go in and out of a business or a company.
The system goes over the transactions one by one, like in an automated teller machine (ATM) that permits only one user at a time
The term “transaction,” in this case, does not only refer to financial aspects, but to requests for information as a whole, such as booking a flight or just logging into an online bank account.

In direct contrast to a transaction processing system is a batch processing system.
This system processes multiple transactions—or a “batch” of transactions—simultaneously.
The main disadvantage is that the processed transactions do not return immediate results.

A modern analogy is a torrent file connected to a folder of files.
The user cannot immediately access a file until all the files in the folder are completely downloaded.
http://www.wisegeek.com/what-is-a-transaction-processing-system.htm