Tuesday, June 19, 2012

Java Interview questions

1. How could Java classes direct program messages to the system console, but error messages, say to a file?
The class System has a variable out that represents the standard output, and the variable err that represents the standard error device. By default, they both point at the system console. This how the standard output could be re-directed:

Stream st =
new Stream (new
FileOutputStream (“techinterviews_com.txt”));
System.setErr(st);
System.setOut(st);

2. What’s the difference between an interface and an abstract class?
An abstract class may contain code in method bodies, which is not allowed in an interface. With abstract classes, you have to inherit your class from it and Java does not allow multiple inheritance. On the other hand, you can implement multiple interfaces in your class.

3. Why would you use a synchronized block vs. synchronized method?
Synchronized blocks place locks for shorter periods than synchronized methods.

4. Explain the usage of the keyword transient?
This keyword indicates that the value of this member variable does not have to be serialized with the object. When the class will be de-serialized, this variable will be initialized with a default value of its data type (i.e. zero for integers).

5. How can you force garbage collection?
You can’t force GC, but could request it by calling System.gc(). JVM does not guarantee that GC will be started immediately.

6. How do you know if an explicit object casting is needed?
If you assign a superclass object to a variable of a subclass’s data type, you need to do explicit casting. For example:

Object a;Customer b; b = (Customer) a;

When you assign a subclass to a variable having a supeclass type, the casting is performed automatically.

7. What’s the difference between the methods sleep() and wait()
The code sleep(1000); puts thread aside for exactly one second. The code wait(1000), causes a wait of up to one second. A thread could stop waiting earlier if it receives the notify() or notifyAll() call. The method wait() is defined in the class Object and the method sleep() is defined in the class Thread.

8. Can you write a Java class that could be used both as an applet as well as an application?
Yes. Add a main() method to the applet.

9. What’s the difference between constructors and other methods?
Constructors must have the same name as the class and can not return a value. They are only called once while regular methods could be called many times.

10. Can you call one constructor from another if a class has multiple constructors
Yes. Use this() syntax.

11. Explain the usage of Java packages.
This is a way to organize files when a project consists of multiple modules. It also helps resolve naming conflicts when different packages have classes with the same names. Packages access level also allows you to protect data from being used by the non-authorized classes.

12. If a class is located in a package, what do you need to change in the OS environment to be able to use it?
You need to add a directory or a jar file that contains the package directories to the CLASSPATH environment variable. Let’s say a class Employee belongs to a package com.xyz.hr; and is located in the file c:/dev/com.xyz.hr.Employee.java. In this case, you’d need to add c:/dev to the variable CLASSPATH. If this class contains the method main(), you could test it from a command prompt window as follows:
c:>java com.xyz.hr.Employee

13. What’s the difference between J2SDK 1.5 and J2SDK 5.0?
There’s no difference, Sun Microsystems just re-branded this version.

14. What would you use to compare two String variables – the operator == or the method equals()?
I’d use the method equals() to compare the values of the Strings and the = = to check if two variables point at the same instance of a String object.

15. Does it matter in what order catch statements for FileNotFoundException and IOExceptipon are written?
A. Yes, it does. The FileNoFoundException is inherited from the IOException. Exception’s subclasses have to be caught first.

16. Can an inner class declared inside of a method access local variables of this method?
It’s possible if these variables are final.

17. What can go wrong if you replace && with & in the following code:
String a=null;
if (a!=null && a.length()>10)
{…}

A single ampersand here would lead to a NullPointerException.

18. What’s the main difference between a Vector and an ArrayList
Java Vector class is internally synchronized and ArrayList is not.

19. When should the method invokeLater()be used?
This method is used to ensure that Swing components are updated through the event-dispatching thread.

20. How can a subclass call a method or a constructor defined in a superclass?
Use the following syntax: super.myMethod(); To call a constructor of the superclass, just write super(); in the first line of the subclass’s constructor.

21. What’s the difference between a queue and a stack?
Stacks works by last-in-first-out rule (LIFO), while queues use the FIFO rule.

22. You can create an abstract class that contains only abstract methods. On the other hand, you can create an interface that declares the same methods. So can you use abstract classes instead of interfaces?
Sometimes. But your class may be a descendent of another class and in this case the interface is your only option.

23. What comes to mind when you hear about a young generation in Java?
Garbage collection.

24. What comes to mind when someone mentions a shallow copy in Java?
Object cloning.

25. If you’re overriding the method equals() of an object, which other method you might also consider?
hashCode()

26. You are planning to do an indexed search in a list of objects. Which of the two Java collections should you use: ArrayList or LinkedList?
ArrayList

27. How would you make a copy of an entire Java object with its state?
Have this class implement Cloneable interface and call its method clone().

28. How can you minimize the need of garbage collection and make the memory use more effective?
Use object pooling and weak object references.

29. There are two classes: A and B. The class B need to inform a class A when some important event has happened. What Java technique would you use to implement it?
If these classes are threads I’d consider notify() or notifyAll(). For regular classes you can use the Observer interface.

30. What access level do you need to specify in the class declaration to ensure that only classes from the same directory can access it?
You do not need to specify any access level, and Java will use a default package access level.

Monday, June 18, 2012

Best Advice 7 Tech Idols Ever Received

The most successful people in every industry, including technology have received a piece of precious advice, at some point in their life from someone, which turned the course of their life and made them what they are today.

 
Richard Parsons, Former chairman of Citigroup considers the advice from Steve Ross, former CEO of Time Warner as his life changing one: "Just remember, it's a small business and a long life. You are going to see all these people again."

 
Business Insider compiled a list of such best tips the executives ever received and shared over the years. Here are some of the lessons received by the tech icons like Bill Gates to new world tech stars like Ben Silberman of Pinterest.

 


Marissa Mayer, VP,Google


 
“My friend Andre said to me, ‘you know, Marissa, you are putting a lot of pressure on yourself to pick the right choice, and I have got to be honest: That is not what I see here. I see a bunch of good choices, and there is the one that you pick and make great’. I think that is one of the best pieces of advice I have ever gotten.”

 
-From an interview with Social Times

 


Eric Schmidt, Google -->


Eric Schmidt, Executive Chairman, Google



“Find a way to say yes to things. Say yes to invitations to a new country, say yes to meet new friends, say yes to learn something new. Yes is how you get your first job, and your next job, and your spouse, and even your kids."



-From the book “The Best Advice I Ever Got” by Katie Couric.



Sheryl Sandberg, Facebook -->


Sheryl Sandberg, COO, Facebook



When Sandberg was thinking she would not accept an offer to be Google’s general manager, Eric Schmidt told her, “Stop being an idiot; all that matters is growth.” She says that is the best advice she ever got.



Larry Page, Google -->


Larry Page, Co-founder, Google



"In graduate school at Stanford University, I had about ten different ideas of things I wanted to do, and one of them was to look at the link structure of the web. My advisor, Terry Winograd, picked that one out and said, 'Well, that one seems like a really good idea.' So I give him credit for that."


-From an interview with CNN Money



Bill Gates, Microsoft -->


Bill Gates, Chairman, Microsoft



"Warren Buffett has taught me a lot of things, but he got me thinking very early on that at some point I'd have the opportunity and responsibility to give the wealth back. And so, literally decades before the foundation got started I was reading about philanthropists from the past … what they'd done and how it worked."



Ben Silberman, Pinterest -->


Ben Silberman, Co-founder, Pinterest



"Don't take too much advice. Most people who have a lot of advice to give - with a few exceptions - generalize whatever they did. ...Every company carves its own path, and (founders) are under pressure to make their startups look like the last successful company everyone remembers."


Richard Branson, Virgin Group -->

Richard Branson, founder and chairman, Virgin Group


 
"My mother always taught me never to look back in regret but to move on to the next thing. The amount of time people waste dwelling on failures rather than putting that energy into another project, always amazes me. I have fun running ALL the Virgin businesses — so a setback is never a bad experience, just a learning curve."

 
-Taken from an interview with The Good Entrepreneur.

Warren Buffett, Chairman and CEO, Berkshire Hathaway


 
Berkshire Hathaway director Thomas Murphy once told “the legendary investor”:

 
"Never forget Warren, you can tell a guy to go to hell tomorrow - you don't give up the right. So just keep your mouth shut today, and see if you feel the same way tomorrow."

 
-From an interview with Yahoo

Microsoft's First Ever Tablet To Be Revealed Today

Microsoft can no longer wait watching iPad eating up the entire PC marketand challenging its software, which runs on millions of systems worldwide. That may be why for the first time after 37-years, the company is going to offer a computer of its own creation.



The device is aimed to kill Apple’s iPad and is expected to be launched in Microsoft’s big party today. According to people familiar with the subject, the tablet is expected to run a new version of Windows and will have access to an e-books store using the Barnes & Noble’s technology. Microsoft on April had announced a strategic partnership with B&N, with an investment of $300 million into the business named “Newco.”

 
Microsoft’s decision to enter into the market is likely to be the outcome of tablet explosion lead by iPads. Tablets are causing a huge threat to the PC market. It has already reached eight percentages in size of PC market and is expected to grow to 40 percent by 2016.

 
With the introduction of device, Microsoft is also following its rival Apple, who was successful in integrating its own hardware and software. "If Microsoft wants to control the entire user experience and the entire quality of their products, they have to build their own hardware," said Michael Cherry, an analyst at Directions on Microsoft, a Redmond-based market research firm.

 
But according to many, Microsoft has a big risk of capturing the tablet market due to the existence of many competes including manufacturers who use Microsoft’s software. There are also new anticipated players into the space including Google, which is redy with its tablet.

 
“If it’s true that Microsoft is going to produce its own tablet, it’s a major turning point for the company and shows just how breathtakingly the landscape has changed in a just a few years,” said Brad Silverberg, a venture capitalist in Seattle and former Microsoft executive.

 
Microsoft’s had earlier failed with Zune, a music player introduced to compete with iPod.

Apple Co-Founder Hates Siri !

Everybody praise Siri,the voice guided personal assistant from Apple. But it seems like one guy doesn’t like its performance at all and unfortunately, the same guy happens to be Apple’s co-founder Steve Wozniac.

Wozniac calls Siri, poo-poo.After Apple made announcements about the enhancements to Siri in its latest WWDC, Wozniac gave his opinions about this software to reporters of Times Union, “A lot of people say Siri. I say poo-poo. I was using it to make reservations long before Apple bought it.”

According to Wozniac, Siri performed better before it was acquired by Apple. As you may recall, Siri was once a third party app on Apple’s App store. At that time it could call taxis, make hotel reservations and even perform complex arithmetic. “I would say, ‘Siri, what are the five largest lakes in California?’ and it would come up, one, two, three, four, five. Then I would ask ‘What are the prime numbers greater than 87?’ and they would come up all in a row. That was pretty incredible,” Wozniak said. He used to talk to his friends and family about the beauty of the app, how it understands like talking to a human and so on.

Then started Siri’s second phase. On seeing the potential of  App, Apple bought the company months after it entered Apple’s store and brought it right into the iPhone 4S. Now, as Wozniac says, “Siri had lost many of its earlier capabilities. If I ask Siri ‘what are the largest lakes in California?’ I would get all these lakefront properties. And I would say, ‘What are the prime numbers greater than 87?’ And I would get prime rib.”

Well, it is the first time Wozniac directly criticize one of the Apple’s prestigious app. But the man who co-founded one of the world’s most reputed company is famous for openly criticizing. Earlier this year, he had talked in favor of android devices and what can it do more than iPhones. But, with Apple’s latest updates to Siri, let’s hope it will become the “future,” that once Wozniac imagined.

America and China More Peaceful than India

The world has become more peaceful for the first time since 2009, says the 2012 Global Peace Index, a study conducted by Institute for Economics and Peace (IEP). India's rank slipped to 142 in comparison with 137 last year. Unites States of America was ranked 88 while China was ranked 89 in the GPI.

 
The Global Peace Index for 158 countries composed of 23 indicators, ranging from measures of civil unrest and crime to military spending, involvement in armed conflict and ties with neighbors. The survey gauged three broad themes, namely the level of safety and security in society, the extent of domestic or international conflict and the degree of militarization.

 
Iceland is the most peaceful country in the world, followed by Denmark and New Zealand. Canada takes the fourth position on the Global Peace Index list while Japan takes the fifth. Somalia remains the world’s least peaceful nation. Afghanistan, Sudan, Iraq and Democratic Republic of Congo round up the bottom five. Israel and Pakistan were also ranked among one of the least peaceful nations.

 
The study also noted that Saarc nations like Nepal, Bhutan and Bangladesh are more peaceful than India. The survey founder Steve Killelea, an Australian entrepreneur said that in the past 12 months India's relation with its neighbors has improved. He added that "We expect this to continue for betterment of the entire region," as reported by TOI.

As per regions, Asia Pacific has an overall score improved by the largest extent from last year and included three of the top five risers. Sub-Saharan Africa is not the least peaceful region for the first time and has steadily increased levels of peacefulness since 2007. Middle East and North Africa were noted to be the least peaceful region, reflecting the upheaval and instability caused by the Arab Spring.

 
Western Europe remains noticeably the most peaceful region with the majority of its countries in the top 20 for the sixth consecutive year. North America showed a slight improvement, continuing a trend since 2007. While Latin America experienced overall gain with 16 of the 23 countries seeing improvements to their GPI score. All regions excluding the Middle East and North Africa saw an improvement in levels of overall peacefulness.

 
The top 5 fallers were noted to be Syria, Egypt, Tunisia, Oman and Malawi. Sri Lanka, Zimbabwe, Bhutan, Guyana and the Philippines were the top 5 risers on the Global Peace index list.

 
There has been a change for the indicators as well. The top three largest improvements were noted for the political terror scale, terrorist acts and military expenditure as a percent of GDP. Improvements were seen in the military sphere, including a decline in military expenditure as a percentage of GDP in 2011, as many countries hit by economic headwinds moved to reduce budget deficits. Six of the world’s top military spenders: Brazil, France, Germany, India, the UK and the U.S., cut their defence budgets in 2011.

West Bengal Government to Set Up IT Hub in Kolkata

Kolkata: West Bengal government will set up an IT hub with private partnership in the south-east suburb of Behala in the metropolis, state Industries and IT Minister Partha Chatterjee said here today.

"Chief Minister Mamata Banerjee has given instructions to look for land at Taratala or adjacent areas for this purpose," Chatterjee said at a function here.

"Techno India Group, Webel and the IT department of the state will jointly set up this hub," he said.

"Creation of jobs is the priority of the Mamata Banerjee government and there are a lot of job potentials in the IT sector," Chatterjee said.

He said the government was also on the look out for land to set up a software and harwdware technology park.

"Talks are on with Intel for setting up the hardware technology park," he said.

He also said that connectivity between the south-eastern suburbs of Behala with the central parts of the city would be much improved with the introduction of the 16.7 km Joka-BBD Bag metro railway. "Services between Joka and Taratala will begin within a year or two in the first phase," he said.

The foundation for the Rs 2619 crore project was laid by President Pratibha Patil on September 22, 2010 and work began on December 29 the same year.

Android this week: Razr beats iPhone; Acer outs HD tablet; Intel phone shows promise



This is the first week I can remember where a single Android phone is reportedly outselling Apple’s iPhone at one carrier. According to a research note from William Blair, that’s exactly what’s happening at Verizon stores. A check of inventory and sales indicated Motorola’s Droid Razr is topping the iPhone 4S, which is good news for Motorola and its new owner, Google.

It’s possible that Verizon’s LTE network is part of this surge for the Android-powered Razr: Without an LTE iPhone on any carrier, the Razr — and other Android phones — can deliver mobile broadband speeds topping 20 Mbps or more; as fast as wired broadband at home. The iPhone 4S holds its own against LTE on HSPA+ networks, but falls far shorter on Verizon and Sprint, where speeds generally average 1.5 Mbps with occasional 3 Mbps bursts. Razr sales could see their own burst as CNet noted this week that Android 4.0 was coming soon to the smartphone.

A700_black-silver-front-back

Android 4.0 is already included with Acer’s new slate:The company this week introduced its A700 tablet with the latest Google software. Pre-sales have begin in the U.S. and Canada at $449 for this 10.1-inch tablet boasting a 1900 x 1200 display with 178-degree viewing angles. I haven’t had the opportunity to try the A700 yet, but it looks good on paper.

Nvidia’s quad-core Tegra 3 powers the A700 which also has one gigabyte of memory and 32 GB of storage, which can be expanded with the microSD card slot. The slate is Wi-Fi only — no 3G/4G radio — but includes Bluetooth, an e-compass and GPS. Acer added Dolby Mobile 3 and 5.1-channel surround sound support and a battery life claim of 8 hours for web surfing or 10.5 hours of video watching; not bad if accurate.

This week I enjoyed reading a review of the Orange San Diego handset. Why? This device, available in Europe, is based on Intel’s smartphone reference design for Android. We’ve waited a long time for Intel to truly get in the smartphone game and the San Diego shows promise for Intel’s Medfield solution.

intel-atom-inside

The Verge wrote the detailed review finding that Intel may not have surpassed ARM-based chips, but in many ways, has at least caught up. I’m not too surprised because the Medfield chip demos on Android devices I saw in Januaryimpressed me enough to say that Intel’s time may have arrived.

The San Diego shines in most performance scenarios and has good stand-by time, but software is the current downfall here; particularly in the camera application. Still, the San Diego is still worth a look as it provides a glimpse into future Android smartphones carrying the “Intel Inside” sticker.