Java6 added a new utility class for reading input data from character
based devices including command line. java.io.Console can be used to read input from command line, but unfortunately it doesn't work on most of IDE like Eclipse and
Netbeans. As per Javadoc call to System.Console() will return attached console to JVM, if it has been started interactive command prompt or it will return null if JVM has been started using a background process or
scheduler job. Anyway java.io.Console not only provides way to read input
from command prompt or Console but also reading passwords from console without echoing
it. Console.readPassword() method reads password and returns a character
array and password is masked during entering so that any peeping tom can
not see your password while you are entering it. here is a code example
of How to read password and input from command prompt or console using java.io.Console. By the way apart from Console, you can also use Scanner or BufferedReader to read input from command prompt, as shown in this example.
Java67 - Java Program Example Tutorial Blog
Java Tutorial Example program tips homework assignment solution, interview question answers eclipse debugging unix linux sql xml blog
Saturday, May 18, 2013
Saturday, April 27, 2013
10 Frequently asked SQL Query Interview Questions
In this article I am giving example of some SQL query which is asked when
you go for interview who is having one or two year experience on this field
.whenever you go for java developer position or any other programmer position
interviewee expect that if you are working from one or two years on any project
definitely you come across to handle this database query, so they test your
skill by asking this type of simple query.
Question 1:
SQL Query to find second highest salary of Employee
Answer : There are many ways to find second highest salary of Employee in
SQL, you can either use SQL Join or Subquery to solve this problem. Here is SQL
query using Subquery :
select MAX(Salary) from Employee WHERE Salary NOT IN (select MAX(Salary) from Employee );
See How
to find second highest salary in SQL for more ways to solve this problem.
Question
2: SQL Query to find Max Salary from each department.
Answer :
SELECT DeptID, MAX(Salary) FROM Employee GROUP BY DeptID.
Question
3:Write SQL Query to display current date.
Ans:SQL has built in function
called GetDate() which returns current timestamp.
SELECT GetDate();
Question
4:Write an SQL Query to check whether date passed to Query is date of given
format or not.
Ans: SQL has IsDate() function which is used to check passed value is
date or not of specified format ,it returns 1(true) or 0(false) accordingly.
SELECT ISDATE('1/08/13') AS "MM/DD/YY";
It will return 0 because passed date is not in correct format.
Question
5: Write a SQL Query to print the name of distinct employee whose DOB is
between 01/01/1960 to 31/12/1975.
Ans:
SELECT DISTINCT EmpName FROM Employees WHERE DOB BETWEEN ‘01/01/1960’ AND ‘31/12/1975’;
Question
6:Write an SQL Query find number of employees according to gender whose DOB is between 01/01/1960 to
31/12/1975.
Answer : SELECT COUNT(*), sex from Employees
WHERE DOB BETWEEN ‘01/01/1960 '
AND ‘31/12/1975’ GROUP BY sex;
Question
7:Write an SQL Query to find employee whose Salary is equal or greater than
10000.
Answer : SELECT EmpName FROM Employees
WHERE Salary>=10000;
Question
8:Write an SQL Query to find name of employee whose name Start with ‘M’
Ans: SELECT * FROM Employees WHERE EmpName like 'M%';
Question
9: find all Employee records containing the word "Joe", regardless of
whether it was stored as JOE, Joe, or joe.
Answer : SELECT * from Employees WHERE upper(EmpName) like
upper('joe%');
Question
10: Write a SQL Query to find year from
date.
Answer : SELECT YEAR(GETDATE()) as "Year";
Hope this article will help you to take a quick practice whenever you are
going to attend any interview and not have much time to go into the deep of
each query.
Other Interview Questions posts from Java67 Blog
Wednesday, April 17, 2013
An Example of Overriding Equals, HashCode and CompareTo method in Java
Though modern IDE like Eclipse, IntelliJ or Netbeans allows you to
generate equals, hashCode and compareTo methods for your value classes, it's
equally important, you know how to do that by hand. By overriding
equals and hashcode method by yourself, you know how they work, what kind
of errors you can get, and most importantly, it's expected form you, as a Java
programmer in any core
Java interview. More often, you would see a coding question in Java, which
ask you to override equals(), hashcode(), compare() and compareTo() methods
for a value class. Since I have already shared some tips on How
to override compareTo method in Java, and couple of example of writing
your own comparator in Java, here I am sharing another simple example of
overriding equals and hashCode methods. If you know rules of overriding equals and hashCode, you might
know that, whenever you override equals, you must have to override hashCode,
otherwise your object will not behave properly on various collection classes
e.g. Map
or Set, which uses equals, compareTo, hashCode to
implement there invariants e.g. Set implementations should not allow any
duplicates.
Sunday, March 31, 2013
How to Split String in Java with Example using Regular Expression
String class provides split() method to split String in
Java, based upon any delimiter, e.g. comma, colon, space or any arbitrary
method. split() method splits string based on
delimiter provided, and return a String
array, which contains individual Strings. Actually, split() method
takes a regular
expression, which in simplest case can be a single word. split() is also overloaded
method in java.lang.String class and its overloaded version
takes a limit parameter which is used to control how many times pattern will be
applied during splitting process. if this limit is positive n, then pattern
will be applied at most n-1 times, if it's negative or zero than split
operation is applied any number of time. For example, if we split String "First,Second,Third" on comma
and provide limit as 2 then pattern will run one time, and split() will
return String array with 2 Strings, "First" and "Second,Third". Since this
method accepts a Java regular expression, it throws PatternSyntaxException, if syntax
of regular expression is invalid.
Saturday, March 23, 2013
How to replace String in Java - character substring replaceAll example
String class in Java provides several methods to replace characters, CharSequence and substring from String in Java. Since String
is immutable in Java, every time you performance an operation on String
either replacement or removing
white space from String, it generates a new String object. There are four
overloaded method to replace String in Java :
replace(char oldChar, char newChar)
replace(CharSequence target, CharSequence
replacement)
replaceAll(String regex, String replacement)
replaceFirst(String regex, String replacement)
Out of these, 2nd one which takes a CharSequence is added
on Java 1.5. CharSequence is actually a super interface for
String,
StringBuffer and StringBuilder in Java, which means you can pass any of String, StringBuffer or StringBuilder Object as
argument to this replace method. replaceFirst() and replaceAll() are very
powerful and accepts regular expression. replaceFirst() only
replace first match, while replaceAll replaces
all matches with replacement String provided.
Tuesday, March 12, 2013
Difference between wait vs notify vs notifyAll in Java
wait, notify, and notifyAll methods are used for inter thread
communication in Java. wait() allows a thread to check for a
condition, and wait if condition doesn't met, while notify() and notifyAll() method
informs waiting thread for rechecking condition, after changing state of shared
variable. One good example of how wait and notify method works is Producer
consumer problem, where one thread produces, and wait if bucket is full;
and other thread consumes and wait if bucket is empty. Both Producer and Consumer
thread, notify each other as well. Producer thread notifies consumer thread
after inserting an item in shared queue, while consumer thread notify producer,
after consuming item from queue. Though Both notify() and notifyAll() are used to notify waiting threads, waiting on
shared queue object, but there are some subtle
difference between notify and notifyAll in Java. Well, when we use notify(), only one
of the sleeping thread will get notification, while in case of notifyAll(), all
sleeping thread on that object will get notified. This concept confuses many
Java programmers, both beginners and experienced alike, Infact this is one of
three question which is very popular on wait and notify concept, along with why
wait and notify is defined in Object class and why
wait and notify called from synchronized method. In this article we will
focus on difference between wait, notify, and notifyAll method in Java.
Friday, March 8, 2013
How to convert Java String to int or Integer - valueOf vs parseInt
There are 3 main ways to convert
String to int in Java, using constructor of Integer class, parseInt() method of java.lang.Integer and Integer.valueOf() method.
Though all those method returns instance of java.lang.Integer, which is
a wrapper class for primitive int value, it's easy to convert Integer to int in
Java. From Java 5, you don't need to do anything, autoboxing
will automatically convert Integer to int. For Java 1.4 or lower version, you
can use intValue() method form java.lang.Integer class, to
convert Integer to int. As name suggest, parseInt() is the
core method to convert String to int in Java. parseInt() accept a String
which must contain decimal digits and first character can be an ASCII minus
sign (-) to denote negative integers. parseInt() throws NumberFormatException, if
provided String is not convertible to int value. By the way parseInt is an overloaded
method and it's overloaded version takes radix or base e.g. 2,8,10 or 16,
which can be used to convert binary, octal, hexadecimal String to int in Java. Integer.valueOf() is another
useful method to convert
String to Integer in Java, it offers caching of Integers from -128 to 127.
Internally, valueOf() also calls parseInt() method for String to int conversion. In this Java programming tutorial, we will see all
three ways to convert String to int value in Java.
Subscribe to:
Posts (Atom)
