Subscribe

RSS Feed (xml)

Powered By

Skin Design:
Free Blogger Skins

Powered by Blogger

Monday, October 5, 2009

J2EE Class -2

WEB BROWSER AND WEB SERVER:

* A web browser lets a user to request a resource.

* The web server gets the request, finds the resource, and returns something back to the user. The returned resource can be a HTML page, a picture, a sound file or even a PDF document. The client asks for the resource and the server sends it back.

In short, A web server takes a client request and gives something back to the client.

REASONS A WEB SERVER DOESN'T SEND A RESOURCE:

* A server doesn't send a resource if its not there, or even if it’s not where the server is expecting it to be. "404 Not Found" is the error response you get when the server can’t find what we asked for.

WEB CLIENT:

* Clients mean both (or either) the human user and the browser application.

* The browser is the piece of software (like Netscape or Mozilla) that knows how to communicate with the server. The browser’s other big job is interpreting the HTML code and rendering the web page for the user.

* The client is the browser application doing what the user asked it to do.

FUNCTIONALITY OF A WEB CLIENT:

A web client lets the user request something on the server, and shows the user the result of the request.

* CLIENTS AND SERVERS COMMUNICATE USING HTML and HTTP.

HTML(HyperText Markup Language):

When a server answers a request,the server usually sends some type of content to the browser so that the browser can display it. Servers often send the browser a set of instructions written in HTML. The HTML tells the browser how to present the content to the user. All web browsers know what to do with HTML, although sometimes an older browser might not understand parts of a page that was written using newer versions of HTML.

HTTP(HyperText Transfer Protocol):

Most of the conversations held on the web between clients and servers are held using the HTTP protocol, which allows for simple request and response conversations. The client sends an HTTP request, and the server answers with an HTTP response.

HTML and HTTP IN SHORT:

if you’re a web server, you speak HTTP.
When a web server sends an HTML page to the client, it sends it using HTTP.

Wednesday, September 30, 2009

J2EE Class -1

Dear friends,

From today I would also like to focus on J2EE concepts.

What all you need to learn:

Besides your brain and a pencil, you also need Java, Tomcat 5, and a
computer.You do not need any Integrated Development Environment like Eclipse, etc. I strongly recommend you, not to use anything but a basic editor until you complete the J2EE tutorial.

GETTING THE SOFTWARE

* If you don’t already Java SE v1.5 or greater, you’ll need it.

* If you don’t already have Tomcat 5, go get it from:http://tomcat.apache.org/

* Select "Tomcat v5.5" in the Downloads menu on the left side of the home page.

* Scroll down to the "Binary Distributions" section and download the version of your
choice. If you do not know, then select the "Core" distribution; it is all you need.

* Save the installation file in a temporary directory.

* Install Tomcat.

--> For Windows, that means double-clicking the install.exe file and following the
installer wizard instructions.

--> For the others, unpack the install file into the place on your hard drive
where you want Tomcat to be.

* To make it easier to follow the book instructions, name the Tomcat home directory
"tomcat".

* Set environment variables for JAVA_HOME and TOMCAT_HOME.

Test Tomcat by launching the tomcat/bin/startup script (which is startup.sh) for
Linux/Unix/OS X. Point your browser to:
http://localhost:8080/ and you’ll see the Tomcat welcome page.

Tuesday, September 29, 2009

The To String method

The toString() Method Override toString() when we want to read something meaningful about the objects of our class. Code can call toString() on our object when it wants to read useful details about our object.

To be more clear, Now when we pass an object reference to the System.out.println() method, and for example, the object's toString() method is called as follows,

public class ClassA {
public static void main (String [] args) {
ClassA classA = new ClassA();
System.out.println(classA);
}
}


then, Running the ClassA class gives us the following output,

% Java ClassA
ClassA@10b62c9


The above output is what we get when we don't override the toString() method of class Object. It gives us the class name (at least that's meaningful) followed by the @ symbol, followed by the unsigned hexadecimal representation of the object's hashcode.

Trying to read this output might motivate us to override the toString() method in our classes, for example,

public class ClassA {
public static void main (String[] args) {
ClassB classB = new ClassB("Test the toString method", new Date());
System.out.println(classB);
}
}
class ClassB {
Date data1;
String data2;
ClassB(String data2, Date data1) {
this.data1 = data1;
this.data2 = data2;
}
public String toString() {
return ("The main aim of this program is to " + data2 +
". This program was written on " + data1);
}

}

The output of this would be a bit more readable:

% Java ClassA
The main aim of this program is to Test the toString method. This program was written on Tue Sep 29 10:26:59 IST 2009


For your Information, Some people refer the toString() method as the "spill-your-guts method," since the most common implementations of toString() simply tell us the object's state.

Thursday, September 24, 2009

Wednesday, September 23, 2009

ABOUT ME

Simple minded learner.

Contact emails:
chandrasekar_cse@yahoo.co.in
s.its.chandru@gmail.com

BLOG's OBJECTIVE

The main Objective Of the Blog is to share everything that I want to share with my readers.The Blog is a repository Of Facts, Health, and most importantly it focuses on Java. Java is a technology which always has something new to offer, no matter how many websites are launched to share it. So, through this blog, I would like to share my Java Experience and supported aptly by Java Quizzes, this is a Repository Of Knowledge which Helps everyone in need in some way or other.

Tuesday, September 22, 2009

Java Quiz -17

Hi friends,

Today I would like to post few more questions to test your capability.
Dont worry guys, I will give you the answer too! Try to answer it yourself without referring to the answers to test yourself.

Q: 1 Given a valid DateFormat object named df, and
1. Date data1 = new Date(0L);
2. String data2 = "September 22, 2009";
3. // insert code here
What updates data1's value with the date represented by data2?

A. 3. data1 = df.parse(data2);
B. 3. data1 = df.getDate(data2);
C. 3. try {
4. data1 = df.parse(data2);
5. } catch(ParseException e) { };
D. 3. try {
4. data1 = df.getDate(data2);
5. } catch(ParseException e) { };

Answer: C

Q: 2 Given:
1. public class ClassA {
2. private StringBuilder testStringBuilder = new StringBuuilder();
3. public void logTest(String data1, String data2) {
4. testStringBuilder.append(data1);
5. testStringBuilder.append(data2);
6. }
7. }
The programmer must guarantee that a single ClassA object works properly for a multi-threaded system. How must this code be changed to be thread-safe?

A. Synchronize the logTest method
B. No change is necessary, the current ClassA code is already thread-safe.
C. replace StringBuilder with just a String object and use the string concatenation (+=) within the logTest method
D. replace StringBuilder with StringBuffer

Answer: A

Q: 3 Given:
1 . import java.io.*;
2. public class ClassA implements Serializable {
3. private ClassB tree = new ClassB();
4. public static void main(String [] args) {
5. ClassA classA = new ClassA();
6. try {
7. FileOutputStream fileOutputStream = new FileOutputStream("ClassA.ser");
8. ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
9. objectOutputStream.writeObject(classA);
10.objectOutputStream.close();
11. } catch (Exception ex) { ex.printStackTrace(); }
12. } }
13.
14. class ClassB { }
What is the result?

A. An exception is thrown at runtime.
B. Compilation fails.
C. An instance of ClassA is serialized.
D. An instance of ClassA and an instance of ClassB are both serialized.

Answer: A

Q: 4 Assuming that the serializeClassC() and the deserializeClassC() methods
will correctly use Java serialization and given:
1. import java.io.*;
2. class ClassA {ClassA() { System.out.print("1"); } }
3. class ClassB extends ClassA implements Serializable {
4. ClassB() { System.out.print("2"); } }
5. public class ClassC extends ClassB { int size = 42;
6. public static void main(String [] args) {
7. ClassC b = new ClassC();
8. b.serializeClassC(b); // assume correct serialization
9. b = b.deserializeClassC(b); // assume correct
10. System.out.println(" restored " + b.size + " "); }
11. // more ClassC methods
12. }
What is the result?

A. Compilation fails.
B. 1 restored 42
C. 12 restored 42
D. 121 restored 42
E. 1212 restored 42
F. An exception is thrown at runtime.

Answer: D

Q: 5 Given this method in a class:
1. public String toString() {
2. StringBuffer testStringBuffer = new StringBuffer();
3. testStringBuffer.append('<');
4. testStringBuffer.append(this.name);
5. testStringBuffer.append('>');
6. return testStringBuffer.toString();
7. }
Which statement is true?

A. This code will perform well and converting the code to use StringBuilder will not enhance the performance.
B. This code will perform poorly. For better performance, the code should be rewritten:
return "<" + this.name + ">";
C. The programmer can replace StringBuffer with StringBuilder with no other changes.
D. This code is NOT thread-safe.

Answer: C

Q: 6 Given:
1. double data = 314159.26;
2. NumberFormat nf = NumberFormat.getInstance(Locale.ITALIAN);
3. String b;
4. //insert code here
Which code, inserted at line 4, sets the value of b to 314.159,26?

A. b = nf.parse( data );
B. b = nf.format( data );
C. b = nf.equals( data );
D. b = nf.parseObject( data );

Answer: B

Q: 7 Given:
System.out.format("Pi is approximately %d.", Math.PI);
What is the result?

A. Pi is approximately 3.141593.
B. Pi is approximately 3.
C. Compilation fails.
D. An exception is thrown at runtime.

Answer: D

Q: 8 Given:
1. public class ClassA implements Comparable {
2. private int data1, data2;
3. public ClassA(int data3, int data4) { data1 = data3; data2 = data4; }
4. public int getTestdata1() { return data1; }
5. public int getTestdata2() { return data2; }
6. public String toString() {
7. return "<" + data1 + "," + data2 + ">";
8. }
9. // insert code here
10. }
Which method will complete this class?

A. public int compareTo(Object object1){/*more code here*/}
B. public int compareTo(ClassA classA){/*more code here*/}
C. public int compare(ClassA classA1,ClassA classA2){/*more code here*/}
D. public int compare(Object object1,Object object2){/*more code here*/}

Answer: B

Q: 9 Given:
1. public class ClassA {
2. private String data1;
3. public ClassA(String data1) {
4. this.data1 = data1;
5. }
6. public boolean equals(Object o) {
7. if ( ! o instanceof ClassA ) return false;
8. ClassA p = (ClassA) o;
9. return p.data1.equals(this.data1);
10. }
11. }
Which statement is true?

A. A HashSet could contain multiple ClassA objects with the same data1.
B. Compilation fails because the hashCode method is not overridden.
C. All ClassA objects will have the same hash code because the hashCode method is not overridden.

Answer: A

Q: 10 Given:
13. public static void search(List listObject) {
14. listObject.clear();
15. listObject.add("data2");
16. listObject.add("data1");
17. listObject.add("data3");
18. System.out.println(Collections.binarySearch(listObject, "data1"));
19. }
What is the result of calling search with a valid List implementation?

A. 0
B. 1
C. 2
D. data1
E. data2
F. data3
G. The result cannot be defined.

Answer: G

Hope you found this useful! Kindly give me your comments regarding the same.Many more Quizzes are to be posted! Kindly make use of them as much as possible!

Monday, September 14, 2009

Java Quiz -16

Hi friends,

Today I would like to post few more questions to test your capability.
Dont worry guys, I will give you the answer too! Try to answer it yourself without referring to the answers to test yourself.

Q: 1 Given a method that must ensure that its parameter is not null:
1. public void test(Object data) {
2. // check for null data
...
10. System.out.println(data.getClass());
11. }
What, inserted at line 2, is the appropriate way to handle a null data?

A. assert data == null;
B. assert data != null, "data is null";
C. if (data == null) {
throw new AssertionException("data is null");
}
D. if (data == null) {
throw new IllegalArgumentException("data is null");
}

Answer: D

Q: 2 Given:
1. package javaQuiz;
2. public class ClassA {
3. public InnerClassB classb = new InnerClassB();
4. class InnerClassB {
5. public int data1;
6. public int data2;
7. }
8. }
Which statement is true about the class of an object that can reference the variable data1?

A. classb can be any class.
B. No class has access to data1.
C. The class must belong to the javaQuiz package.
D. The class must be a subclass of the class ClassA.

Answer: C

Q: 3 Which four statements are true? (Choose four.)

A. Has-a relationships can be implemented using instance variables.
B. Is-a relationships can be implemented using the extends keyword.
C. Has-a relationships should never be encapsulated.
D. An array or a collection can be used to implement a one-to-many has-a relationship.
E. The relationship between Movie and Actress is an example of an is-a relationship.
F. Is-a relationships can be implemented using the implements keyword.
G. Has-a relationships should be implemented using inheritance.

Answer: A, B, D, F

Q: 4 Given:
0. public class ClassA {
1.
2. private String data1;
3. private Integer data2;
4. public String data3;
5.
6. public void test(String data1,
7. String data3,
8. Integer data2) {
9. this.data1 = data1;
10. this.data3 = data3;
11. this.data2 = data2;
12. }
13. }
Which statement is true?

A. The code demonstrates polymorphism.
B. The class is fully encapsulated.
C. The data3 variable breaks encapsulation.
D. The data1 and data2 variables break polymorphism.
E. The test method breaks encapsulation.

Answer: C

Q: 5 Given:
0. abstract class ClassA {
1. abstract void test1();
2. void test2() { }
3. }
4. class ClassB extends ClassA {
5. void test1() { }
6. void test2() { }
7. }
8. class ClassC extends ClassB { void test3() { } }
and:
ClassA classA = new ClassB();
ClassC classB = new ClassC();
ClassA classC = new ClassC();
What are four valid examples of polymorphic method calls? (Choose four.)

A. classA.test2();
B. classC.test2();
C. classC.test3();
D. classC.test1();
E. classB.test3();
F. classA.test1();

Answer: A, B, D, F

Q: 6 Given:
1. interface ClassA { public void test(); }
...
10. class ClassB {}
...
20. class ClassC extends ClassB {
21. ClassD classD;
22. }
...
30. class ClassE extends ClassC implements ClassA{
31. public void test() {}
32. }
...
40. class ClassF implements ClassA{
41. public void test() {}
42. }
Which three are true? (Choose three.)

A. ClassF is-a ClassB
B. ClassF is-a ClassA
C. ClassC is-a ClassB
D. ClassC is-a ClassA
E. ClassF has-a ClassB
F. ClassE has-a ClassD
G. ClassE has-a ClassA

Answer: B, C, F

Q: 7 Given:
1. public class ClassA {
2. protected int test(int data) { return 0; }
3. }
4. class ClassB extends ClassA {
5. // insert code here
6. }
Which five methods, inserted independently at line 5, will compile? (Choose five.)

A. public int test(int data1) { return 0; }
B. private int test(int data1) { return 0; }
C. private int test(long data1) { return 0; }
D. protected long test(int data1) { return 0; }
E. protected int test(long data1) { return 0; }
F. protected long test(long data1) { return 0; }
G. protected long test(int data1, int data2) { return 0; }

Answer: A, C, E, F, G

Q: 8 Given:
1. public class ClassA {
2. protected static int test(int data1, int data2) { return data1 * data2;}
3. }
and:
10. public class ClassB extends ClassA{
11. public static int test(int data1, int data2) {
12. int data3 = super.test(data1, data2);
13. return data3;
14. }
15. }
and:
20. ClassB classB = new ClassB ();
21. System.out.println(classB.test(3,4));
22. System.out.println(ClassB.test(2,2));
What is the result?

A. 12
4
B. The code runs with no output.
C. An exception is thrown at runtime.
D. Compilation fails because of an error in line 11.
E. Compilation fails because of an error in line 12.
F. Compilation fails because of an error in line 21.

Answer: E

Q: 9 Given:
1. class ClassA {
2. java.util.ArrayList data1;
3. public final void testData(String data2) {
4. data1.add(data2);
5. }
6. }
7. public class ClassB extends ClassA {
8. public void testData(String data2) {
9. System.out.println("Cannot add Data1");
10. }
11. public static void main(String[] args) {
12. ClassA classA = new ClassB();
13. classA.testData("Data2");
14. }
15. }
What is the result?

A. The code runs with no output.
B. A NullPointerException is thrown in Line 4.
C. Compilation fails.
D. Cannot add Data1

Answer: C

Q: 10 Which three statements are true? (Choose three.)

A. A final method in class ClassA can be abstract if and only if ClassA is abstract.
B. A protected method in class ClassA can be overridden by any subclass of ClassA.
C. A private static method can be called only within other static methods in class ClassA.
D. A non-static public final method in class ClassA can be overridden in any subclass of ClassA.
E. A public static method in class ClassA can be called by a subclass of ClassA without explicitly referencing the class ClassA.
F. A method with the same signature as a private final method in class ClassA can be implemented in a subclass of ClassA.
G. A protected method in class ClassA can be overridden by a subclass only if the subclass is in the same package as ClassA.

Answer: B, E, F

Hope you found this useful! Kindly give me your comments regarding the same.Many more Quizzes are yet to be posted! Kindly make use of them as much as possible!

Friday, September 11, 2009

Testing and Concepts

Hi friends,

Today I would like to present few points related to Testing and some new Concepts related to Java.I assure you that it will be very informative for the readers.

Round : Testing

1.What is Black Box testing and white box testing?

Black Box Testing:
Also known as functional testing. A software testing technique whereby the internal workings of the item being tested are not known by the tester.

White Box Testing:
Also known as glass box, structural, clear box and open box testing. A software testing technique whereby explicit knowledge of the internal workings of the item being tested are used to select the test data. Unlike black box testing, white box testing uses specific knowledge of programming code to examine outputs.

2.What is the name of the testing supposed to be written by the developers?
White Box Testing.

3.Give the name of 2 performance of testing tools?
Apache Jmeter,curl loader.

4.Give the name of 2 automated testing tools?
QTP and Winrunner,Rational Robot.

5.Difference between stress testing and load testing?
Stress Testing : To test how the application is responding while we enter maximum length of strings into the application.
Load Testing : To test the application respond(response time) by increasing the number of users.



Round:Concepts

1.Namespace
As a rule, names in a namespace cannot have more than one meaning, that is, two or more things cannot share the same name.

2.Lazy loading
Lazy loading, also known as dynamic function loading , is a mode that allows a developer to specify what components of a program should not be loaded into storage by default when a program is started. Ordinarily, the system loader automatically loads the initial program and all of its dependent components at the same time. In lazy loading, dependents are only loaded as they are specifically requested. Lazy loading can be used to improve the performance of a program if most of the dependent components are never actually used.

3.Two Phase Commit
The two-phase commit strategy is designed to ensure that either all the databases are updated or none of them, so that the databases remain synchronized.

4.Server affinity
Server affinity refers to the characteristics of each load distribution facility that take these constraints into account. The load distribution facility recognizes that multiple servers can be acceptable targets for a request. However, it also recognizes that each request can be directed to a particular server where it is handled better or faster.

Server affinity can be weak or strong.

In weak server affinity, the system attempts to enforce the desired affinity for the majority of requests, but does not always guarantee that this affinity will be respected.
In strong server affinity, the system guarantees that affinity is always respected and generates an error when it cannot direct a request to the appropriate server.

5.Soft Deletion.
There also has to be a special handling of the delete operation. Not every client has enough space for every database item. Sometimes we want to remove an item from just one client, but not from the others.
Introduce a flag to an existing table which indicates that a row has been deleted (this is called a soft/logical delete) instead of actually deleting the row (a hard delete).

Hope this topic was useful to you, Please Post your comments!

Thursday, September 10, 2009

About Product,Companies and Databases

Hi friends,

Today I would like to present few points related to Product,Companies and Databases which would be helpful for the readers.

Round:About Product and Companies

1.How is the company that tells the “System applications and products for data processing”better known as?
SAP

2.Five colleagues working for patni computers ,resigned their job and started a company with an investment of 10000.Today this company is a house hold name. Which company we are talking about?
Infosys

3.Mark Shuttleworth is a South African entrepreneur who was the second self funded space tourist. Which software product is he responsible for?

Ubuntu

4.Which product is advertised “Hello coming in june”.This product is now a rage and set the benchmark in its category.
Ipod


5.Microsoft is accused by Apple to have copies of Apple's windows or gui concept. From which company/organization did Apple copy it from?
Xerox


Round:Databases

1.What is surrogate key?
A surrogate key in a is a unique identifier for either an entity in the modeled world or an object in the database. The surrogate key is not derived from application data.
A surrogate should have the following
the value is unique system-wide, hence never reused;
the value is system generated;
the value is not manipulable by the user or application;
the value contains no semantic meaning;
the value is not visible to the user or application;


2.What is the difference between union and unionall?
Union will filter duplicate values but union all will not filter duplicate values.


3.Every non-key attribute if fully functionally dependent on primary key.Which normal form it is?
2nd Normal form.


4.What is the difference between primary key and unique key?
The column holding the primary key constraint cannot accept null values.whereas column holding the unique constraint can accept null values .

5.What is the difference between DELETE TABLE and TRUNCATE TABLE?
TRUNCATE is a DDL command and cannot be rolled back. All of the memory space is released back to the server.
DELETE is a DML command and can be rolled back.

Hope this topic was useful to you, Please Post your comments!

Wednesday, September 9, 2009

People and Java code debugging

Hi friends,

Today I would like to present few concepts in People related to Open Source and Java code debugging which would be helpful for the readers.

Without wasting further time, Here We Go!
Round :People

1.Who created Java?
James Gosling(born May 19, 1955 near Calgary, Alberta, Canada).

2.Who invented C++?
Bjarne Stroutstrup.

3.Who is the well known founder of Oracle?
Lawrence Joseph Larry Ellison.

4.Who is the well known founder of Apple?
Steven Paul Jobs.

5.He is from Moolaipozhi village near Tirunelvelli.Went on to start an IT company which is among top ten Indian IT services firms.
Who is this man and which company he create?

Shiv Nadar - HCL

Round : Java code debugging

1.public class Test {
public static void main(String[] args) throws Exception{
BigInteger fiveThousand = new BigInteger("5000");
BigInteger fiftyThousand = new BigInteger("50000");
BigInteger fiveHundredThousand = new BigInteger("500000");
BigInteger Total =BigInteger.ZERO;
System.out.println(Total);
Total.add(fiveThousand);
Total.add(fiftyThousand);
Total.add(fiveHundredThousand);
System.out.println(Total);
}
}
Output:0


2.package com.planetsoft;
public class Me{
public static void main(String[] args){
System.out.println(Me.class.getName().replaceAll(“.”,”\”)+”.class”);
}
output://///////////////.class


3..public boolean isOdd(int i){
return i%2==1;
}
Output: if we give odd numbers with negative it will return false . For eg: Lets take the value of i=-3 , after computing i%2 the value will be -1 and it will check -1==1 ,hence both not equals it will return false.


4..Write a one line method that takes a list and returns a list , eliminating duplicates and preserving the order of the original list?
Let duplicatedList be a Array list variable with duplicate values.whereas
LinkedHashSet list = new inkedHashSet(duplicatedList);



5. Write a one line method that takes comma a demilited strings and return a string[],each comma may be followed by zero or more spaces which must be ignored by the method.
It can be achieved using the regular expression.Here is the example using String,Similar way we can do that for string array.
String s = ", There , , a , is a , , , null , object ,";
s=s.replaceAll("%*, *","" );
System.out.println(s);



1.public class TestList {
public static void main(String[] args) throws Exception{
final long micros_perDay = 24*60*60*1000*1000;
final long macros_perDay = 24*60*60*1000;
System.out.println(micros_perDay/macros_perDay);
}
} output:5


2.public class AnimalFarm {
public static void main(String[] args) throws IOException{
final String pig = "length: 10";
final String pig1 = "length :"+pig.length();
System.out.println("animals are equal"+pig==pig1);

}
}
output:false


3.public class SetMagic {
public static void main(String[] args) throws IOException{
int[] vals = {10,012,14,016};
Set magic = new HashSet();
for(int i=0;i magic.add(vals[i]);
}System.out.println(magic.size());
}
}

output:2

4.public class Increment {
public static void main(String[] args){
int j=0;
for(int i=0;i<100;i++){
j=j++;
}
System.out.println(j);
}
}

output:0

5.Provide a definition for i so that the loop turns into a infinite loop.
while(i!=i){
system.out.println(“please stop”);
}
output: we can declare i
double i=Double.Nan;

Hope this topic was useful to you, Please Post your comments!

Monday, September 7, 2009

Java Quiz -15

Hi friends,

Today I would like to present few concepts in Open Source and Java API's in today's Quiz which would be helpful for the readers.

Without wasting further time, Here We Go!

Round : Open Source Round

1.Who started the open source movement?
Richard Stallman

2.What does”free as freedom, free as in beer” means in the context of Open Source?
“free as freedom” phrase describes that the users can modify the functionality of the open source.
“free as in beer” phrase describes that the users can get the liscence free of cost.


3.Give the name three Open Source softwares?
UNIX,LINUX,Perl

4.Give the name of three open source databases?
Hsql,MySql,PostgreSQL


5.What is the name of open source webserver which is most widely used webserver in the world?
Apache,Jakarta Tomcat,Tornado

Round : Java API's Round

1. How do you achieve custom serialization?
We can achieve this by overriding the writeObject() and readObject() methods in ObjectOutputStream and ObjectInputStream.

2. I have an int column in the database that is null.
I do a rs.getInt() on that column and get a 0.
How do u find out whether the column has a value of 0 or its null?

Using the rs.wasNull() will return a boolean.

3.I have a list of String objects.
I want to create string array from the list not an object array.
How do u achieve this?

ArrayList a1 = new ArrayList();
a1.add("1");
a1.add("2");
a1.add("3");
String st1[] = new String[a.size()];
for(int i=0; i st1[i]=a1.get(i);
}

Hope you found this useful! Kindly give me your comments regarding the same.Many more Quizzes are yet to be posted! Kindly make use of them as much as possible!

Friday, September 4, 2009

Round :Basic Computer Science

Hi friends,

Its been sometime since we shared some information regarding the concepts(For those who missed out, We had been doing Few Java Quizzes! Check It out Here 
http://helptotheneeded.blogspot.com/search/label/Java%20Quiz)


Today I would like to present few concepts in basic Computer Science which would be helpful for the readers

Without wasting further time, Here We Go!

1.Give names of two sorting algorithms
  Bubble sort,Insertion sort,Shell sort,Merge sort,Heap sort,Quick sort,Radix sort,Bucket sort,Distribution sort,Shuffle sort.

2.What is the name of the style of programming where the function calls itself?
  Recursion

3.Name any two searching algorithms
  Breadth-First search,Depth limited search,Kruskal's algorithms,Prim's algorithms,Grover's algorithms,Dijikistra's algorithms.

4.What is Pointer?
  A pointer is a variable which contains the address in memory of another variable.

5.Give two names of typical data structure:
  Stack,Queue,Linked list,Doubly Linked List.

6.What is priority queue?
  An element's priority is the time that element was inserted.

7.What is a binary tree?
  A binary tree is a in which each node has at most two .
  -The left of a node contains only nodes with keys less than the node's key. -The right subtree of a node contains only nodes with keys greater than node's key.Both the left and right subtrees must also be binary search trees. 

8.What is a self Referential structure?
  It is a important characteristic of the data structures used to implement lists , is that they contain, as a member, a reference variable of the same type as the class itself. For this reason, these data structures are frequently called self-referential or recursive data structures .

9.What is/are difference(s) between tree and graph?
  -Tree has no loop but graph has loops. 
  -Tree always has direction but graph do not have direction.

10.What is the complexity of the Big O notation in the following pseudo code:
 n=100;
 for(i=O;i  do something;
 }
  The complexity of the Big O notation is o(n)..

Thursday, September 3, 2009

Java Acronyms

Hi friends,

Its been sometime since we shared some information regarding the concepts(For those who missed out, We had been doing Few Java Quizzes! Check It out Here 
http://helptotheneeded.blogspot.com/search/label/Java%20Quiz)


Today I would like to present few acronyms which would be helpful for the readers

Without wasting further time, Here We Go!

Round :Acronyms 


1.BPM – Business Process Management
-a field of management focused on aligning organizations with the wants and needs of clients.

2.JAXB- Java Architecture for XML Binding
-XML and Java technology are recognized as ideal building blocks for developing Web services and applications that access Web services. A new Java API called Java Architecture for XML Binding (JAXB) can make it easier to access XML documents from applications written in the Java programming language.

3.SOA-Service Oriented Architecture
-a computer systems architectural style for creating and using business processes, packaged as services

4.ORM-Object Relational Mapping
  -a software-programming issue in linking object-oriented code with relational databases .

5.XP – Extreme Programming
  -is a software engineering methodology which is intended to  
  improve software quality and responsiveness to changing 
  customer requirements.

6.3NF-Third Normal Form
  -In order to be in a third normal form,a relation must fulfill  
  the requirements to be in second normal form .Additionally all  
  attributes that are not dependent upon the primary key must be  
  eliminated.

7.MD5 – Message Digest Algorithm 5.
-In cryptography, MD5 (Message-Digest algorithm 5) is a widely used cryptographic hash function with a 128-bit hash value.

8.XSLT – Extensible Stylesheet Language Transformation.
  - a style sheet language for XML documents.

9.SAAS – Software As a Service.
-The sharing of end-user licenses and on-demand use may also reduce investment in server hardware or the shift of server use to SaaS suppliers of applications file services.

10.AOP – Aspect Oriented Programming

is a programming paradigm that increases modularity by allowing the separation of cross-cutting concerns, forming a basis for aspect-oriented software development.

Hope this topic was useful to you, Please Post your comments!

Wednesday, September 2, 2009

Java Quiz -14

Hi friends, 

Today I would like to post few more questions to test your capability. 
Dont worry guys, I will give you the answer too! Try to answer it yourself without referring to the answers to test yourself.

Q: 1 Given:

1. public class ClassA {
2. private int data1;
3. public void test1() {
4. int tips = data1;
5. data1 = tips + 1;
6. }
7. public void test2() {
8. for(int data2 = 0; data2 < 5; data2++) {
9. new Thread() {
10. public void test2() {
11. test1();
12. System.out.print(data1 + ", ");
13. } }.start();
14. } }
Which two changes, taken together, would guarantee the output: 1, 2, 3, 4, 5, ?

A. move the line 12 print statement into the test1() method
B. change line 7 to public synchronized void test2() {
C. change the variable declaration on line 2 to private volatile int data1;
D. wrap the code inside the test1() method with a synchronized( this ) block
E. wrap the for loop code inside the test2() method with a synchronized block synchronized(this) { // for loop
code here }

Answer: A, D

Q: 2 Given:
classA1 and classA2 are public references available to many other threads. classA1 refers to a Thread and classA2 is an
Object. The thread classA1 is currently executing classA2.wait().
From another thread, what provides the most reliable way to ensure that classA1 will stop executing wait()?

A. classA1.notifyAll();
B. classA1.notify();
C. classA2.notify();
D. Object.notify();
E. Thread.notify();
F. classA2.notifyAll();

Answer: F

Q: 3 Given:
1. static void test() throws RuntimeException {
2. try {
3. System.out.print("test ");
4. throw new RuntimeException();
5. }
6. catch (Exception ex) { System.out.print("exception1 "); }
7. }
8. public static void main(String[] args) {
9. try { test(); }
10. catch (RuntimeException ex) { System.out.print("exception2 "); }
11. System.out.print("data ");
12. }
What is the result?

A. test data
B. A Throwable is thrown by main at exception2.
C. test exception2 data
D. test exception data
E. Compilation fails.

Answer: D

Q: 4 Given:
1. Float pi = new Float(10.14f);
2. if (pi > 10) {
3. System.out.print("pi is bigger than 10. ");
4. }
5. else {
6. System.out.print("pi is not bigger than 10. ");
7. }
8. finally {
9. System.out.println("Answer it yourself.");
10. }
What is the result?

A. An exception occurs at runtime.
B. pi is bigger than 10.
C. Compilation fails.
D. pi is bigger than 10. Answer it yourself.
E. pi is not bigger than 10. Answer it yourself.

Answer: C

Q: 5 Given:
1. public static Iterator test(List listdata) {
2. Collections.test(listdata);
3. return listdata.iterator();
4. }
5. public static void main(String[] args) {
6. List listdata = new ArrayList();
7. listdata.add("1"); listdata.add("2"); listdata.add("3");
8. for (Object objdata: test(listdata))
9. System.out.print(objdata + ", ");
10. }
What is the result?

A. Compilation fails.
B. 1, 2, 3,
C. 3, 2, 1,
D. The code runs with no output.
E. An exception is thrown at runtime.

Answer: A

Q: 6 Given:
1. class ClassA {
2. public void process() { System.out.print("ClassA,"); }
3. class ClassB extends ClassA {
4. public void process() throws IOException {
5. super.process();
6. System.out.print("ClassB,");
7. throw new IOException();
8. }
9. public static void main(String[] args) {
10. try { new ClassB().process(); }
11. catch (IOException e) { System.out.println("Exception"); }
What is the result?

A. Exception
B. ClassA,ClassB,Exception
C. Compilation fails because of an error in line 10.
D. Compilation fails because of an error in line 4.
E. ClassA NullPointerException is thrown at runtime.

Answer: D

Q: 7 Given:
1. try {
2. // some code here
3. } catch (NullPointerException e1) {
4. System.out.print("exception1");
5. } catch (RuntimeException e2) {
6. System.out.print("exception2");
7. } finally {
8. System.out.print("exception3");
9. }
What is the result if exception1 NullPointerException occurs on line 34?

A. exception3
B. exception1
C. exception1exception2
D. exception1exception3
E. exception2exception3
F. exception1exception2exception3

Answer: D

Q: 8 Given:
1. public static Collection get() {
2. Collection sorted = new LinkedList();
3. sorted.add("data2"); sorted.add("data3"); sorted.add("data1");
4. return sorted;
5. }
6. public static void main(String[] args) {
7. for (Object obj: get()) {
8. System.out.print(obj + ", ");
9. }
10. }
What is the result?

A. data1, data2, data3,
B. data2, data3, data1,
C. An exception is thrown at runtime.
D. Compilation fails.
E. The code runs with no output.

Answer: B

Q: 9 Given:
1. int z = 5;
2.
3. public void stuff1(int x) {
4. assert (x > 0);
5. switch(x) {
6. case 2: x = 3;
7. default: assert false; } }
8.
9. private void stuff2(int y) { assert (y < 0); }
10.
11. private void stuff3() { assert (stuff4()); }
12.
13. private boolean stuff4() { z = 6; return false; }
Which statement is true?

A. Only the assert statement on line 9 is used appropriately.
B. All of the assert statements are used appropriately.
C. The assert statements on lines 7 and 9 are used appropriately.
D. The assert statements on lines 4 and 7 are used appropriately.
E. The assert statements on lines 7 and 11 are used appropriately.
F. The assert statements on lines 7, 9, and 11 are used appropriately.
G. The assert statements on lines 4, 7, and 9 are used appropriately.

Answer: C

Q: 10 Given:
1. public class Test {
2. public static void main(String [] args) {
3. int data1 = 5;
4. boolean data2 = true;
5. boolean data3 = false;
6.
7. if ((data1 == 4) && !data3 )
8. System.out.print("test1 ");
9. System.out.print("test2 ");
10. if ((data3 = true) && data2 )
11. System.out.print("test3 ");
12. }
13. }
What is the result?

A. test2
B. test3
C. test1 test2
D. test2 test3
E. test1 test2 test3
F. An exception is thrown at runtime.
G. Compilation fails.

Answer: D

Hope you found this useful! Kindly give me your comments regarding the same.Many more Quizzes are yet to be posted! Kindly make use of them as much as possible! 

Tuesday, September 1, 2009

Java Quiz -13

Hi friends, 

Today I would like to post few more questions to test your capability. 
Dont worry guys, I will give you the answer too! Try to answer it yourself without referring to the answers to test yourself.

Q: 1 Given:

1. void waitTest() {
2. Object obj = new Object();
3. synchronized (Thread.currentThread()) {
4. obj.wait();
5. obj.notify();
6. }
7. }
Which statement is true?

A. This code may throw an InterruptedException.
B. This code may throw an IllegalStateException.
C. This code may throw a TimeoutException after ten minutes.
D. This code will not compile unless "obj.wait()" is replaced with "((Thread) obj).wait()".
E. Reversing the order of obj.wait() and obj.notify() may cause this method to complete normally.

F. A call to notify() or notifyAll() from another thread may cause this method to complete normally.

Answer: B

Q: 2 Given:
1. public class TestClassA implements Runnable {
2. public static void main (String[] args) throws Exception {
3. Thread t = new Thread(new TestClassA());
4. t.start();
5. System.out.print("Started");
6. t.join();
7. System.out.print("Complete");
8. }
9. public void run() {
10. for (int test = 0; test < 4; test++) {
11. System.out.print(test);
12. }
13. }
14. }
What can be a result?

A. An exception is thrown at runtime.
B. The code executes and prints "Started0123Complete".
C. The code executes and prints "StartedComplete".
D. Compilation fails.
E. The code executes and prints "StartedComplete0123".


Answer:B

Q: 3 Which ClassB code fragments will execute the method doData() in a separate
thread? (Choose two.)
A. new Thread() {
public void run() { doData(); }
};
B. new Thread() {
public void start() { doData(); }
};
C. new Thread() {
public void start() { doData(); }
}.run();
D. new Thread() {
public void run() { doData(); }
}.start();
E. new Thread(new Runnable() {
public void run() { doData(); }
}).run();
F. new Thread(new Runnable() {
public void run() { doData(); }
}).start();


Answer: D, F

Q: 4 Given:
1. interface A { public int getData(); }
2. class ClassB implements A {
3. public int getData() { return 1; }
4. }
5. class ClassC extends B {
6. // insert code here
7. }
Which ClassC code fragments, inserted individually at line 4, make use of polymorphism? (Choose three.)
A. public void add( ClassC c) { c.getData(); }
B. public void add( ClassB b) { b.getData(); }
C. public void add(A a) { a.getData(); }
D. public void add(A a, ClassB b) { a.getData(); }
E. public void add( ClassC c1, ClassC c2) { c1.getData(); }


Answer: B, C, D

Q: 5 Given:
1. public class ClassA {
2.
3. private String design;
4. private Integer data;
5. public String test;
6.
7. public void setClassdata(String design,
8. String test,
9. Integer data) {
10. this.design = design;
11. this.test = test;
12. this.data = data;
13. }
14. }
Which statement is true?
A. The class is fully encapsulated.
B. The code demonstrates polymorphism.
C. The test variable breaks encapsulation.
D. The design and data variables break polymorphism.
E. The setClassdata method breaks encapsulation.


Answer: C

Q: 6 Given:
1. package test;
2.
3. class ClassA {
4. public String data = "Java Quiz";
5. }
What can directly access and change the value of the variable data?
A. any class
B. only the ClassA class
C. any class in the test package
D. any class that extends ClassA


Answer: C

Q: 7 Given:
1. class ClassA { public String quiz() { return "answers"; } }
2. class ClassB extends ClassA {
3. public String quiz() { return "Test Answers"; }
4. }
5. class ClassC extends ClassA {
6. public String quiz() { return "Test Data"; }
7. }
...
11. ClassA ClassA = new ClassB();
12. ClassC ClassC = (ClassC)ClassA;
13. System.out.println(ClassC.quiz());
What is the result?
A. answers
B. Test Answers
C. Test Data
D. Compilation fails.
E. An exception is thrown at runtime.


Answer: E

Q: 8 Which three statements are true?
A. A final method in class ClassA can be abstract if and only if ClassA is abstract.
B. A protected method in class ClassA can be overridden by any subclass of ClassA.
C. A private static method can be called only within other static methods in class ClassA.
D. A non-static public final method in class ClassA can be overridden in any subclass of ClassA.
E. A public static method in class ClassA can be called by a subclass of ClassA without explicitly referencing the class ClassA.
F. A method with the same signature as a private final method in class ClassA can be implemented in a subclass of ClassA.
G. A protected method in class ClassA can be overridden by a subclass of A only if the subclass is in the same package as ClassA.


Answer: B, E, F

Q: 9 Which two statements are true about has-a and is-a relationships? 
A. Inheritance represents a has-a relationship.
B. Inheritance represents an is-a relationship.
C. Instance variables can be used when creating a has-a relationship.
D. Interfaces must be used when creating a has-a relationship.


Answer: B, C

Q: 10 Given:
11. Runnable r = new Runnable() {
12. public void testData() {
13. System.out.print("Test Quiz Data");
14. }
15. };
16. Thread t = new Thread(r) {
17. public void testData() {
18. System.out.print("Answer it yourself");
19. }
20. };
21. t.start();
What is the result?
A. Test Quiz Data
B. Answer it yourself
C. Compilation fails.
D. The code runs with no output.
E. An exception is thrown at runtime.


Answer: B

Hope you found this useful! Kindly give me your comments regarding the same.Many more Quizzes are yet to be posted! Kindly make use of them as much as possible! 

Monday, August 31, 2009

Preparing For SCJP Certification

Hi friends,

Today I would like to share my experience both during my preparation and on the day of the SCJP exam which I took on the previous Saturday(29/08/2009).

POINT TO NOTE:

There are totally seven parts that evaluate a candidate in an SCJP test.

* Declaration,Initialization and scoping
* Flow Control
* API Contents
* Concurrency
* OO Concepts
* Collections/Generics
* Fundamentals

PREPARATION:

First of all I took my certification seriously as this is going to be my first stepping stone.I studied smart and utilized as much time as possible, for the exam.

I started my preparation about five months back. I started with the book
SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310-055) by Katherine Sierra and Bert Bates

To get my Fundamentals strong, I referred the book,
Head First Java by Katherine Sierra and Bert Bates
Both these books cover all the portions that are needed for the SCJP 1.5 exam.
Head first Java was very helpful to understand the concepts and was a good support when I needed a concept in SCJP book to be explained in more detail.

I also had the help of many of our associates who taught me how to view a problem in the exam.
Two typical examples, 
Before starting, I find out problems that can be easily solved and solve them.I solve the rest when I go through it the next time. This ensures that I have a favourable number questions that help me clear the exam, and in the second pass, I concentrate on the rest to ensure that I get as many questions right as possible and score high.

If I was spending some time on a problem, I make sure that I complete it before proceeding further. This would save a lot of time for going through all the questions again.

Apart from that I referred to various Mock Tests available online and I made sure that I didnt go by their solution if I felt that the answer given to be confusing.
This helped me a lot and I learnt something new each time I attended one.
I used to solve the code snippets and had found some examples which were given wrong solutions.


DURING THE EXAM:

During the exam, I felt the same tension that I had during my degree exam. But I would assure you that you dont have to worry, there is ample time. Three hours for sixty questions and you have choice for the answers too!

There are two type of questions according to my classification,
One type is that we have to judge what is the output from a set of choices, and 
In Second type, you are given some code snippets and are asked to arrange them in a proper sequence so that you get some specific output.
Especially for the second type, the online questions which I had tested myself earlier came to my help and I solved them easily.

Remember that you are not given ant or eclipse in the exam. Everything has to be done by your mind and the paper pad.

I completed my test in about one and a half hours. At the end of one and a half hours, I was confident of fifty five questions out of sixty to be correct.

This confidence made me bold and I tried my best on the other five questions.
At the end, As I submitted I got the result immediately and all my tension got subsided on seeing the result.


MY TOTAL SCORE  93.00%

ASSESMENT SECTION
SECTION ANALYSIS



Declaration,Initialization and scoping  90.00%
Flow Control  81.00%
API Contents  100.00%
Concurrency  100.00%
OO Concepts  90.00%
Collections/Generics  100.00%
Fundamentals  100.00%


Sunday, August 30, 2009

Java Quiz-12

Hi friends, 

Today I would like to post few more questions to test your capability. 
Dont worry guys, I will give you the answer too! Try to answer it yourself without referring to the answers to test yourself.

Q: 1 Given

1. // insert code here
2. private N min, max;
3. public N getMin() { return min; }
4. public N getMax() { return max; }
5. public void add(N added) {
6. if (min == null || added.doubleValue() < min.doubleValue()) 
7. min = added;
8. if (max == null || added.doubleValue() > max.doubleValue()) 
9. max = added;
10. }
Which two, inserted at line 1, will allow the code to compile? (Choose two.)

A. public class ClassA {
B. public class ClassA {
C. public class ClassA {
D. public class ClassA {
E. public class ClassA {
F. public class ClassA {

Answer: D, F

Q: 2 Given:
1. import java.util.*;
2.
3. public class ClassA{
4. public static void main(String[] args) {
5. ArrayList strings = new ArrayList();
6. strings.add("aAaA");
7. strings.add("AaA");
8. strings.add("aAa");
9. strings.add("AAaa");
10. Collections.sort(strings);
11. for (String data : strings) { System.out.print(data + " "); }
12. }
13. }
What is the result?

A. Compilation fails.
B. aAaA aAa AAaa AaA
C. AAaa AaA aAa aAaA
D. AaA AAaa aAaA aAa
E. aAa AaA aAaA AAaa
F. An exception is thrown at runtime.

Answer: C

Q: 3 Given:
1. public abstract class ClassA {
2. private int x;
3. private int y;
4. public abstract void quiz();
5. public void test(int x, int y) {
6. this.x = x;
7. this.y = y;
8. }
9. }
Which two classes use the ClassA class correctly? (Choose two.)
A. public class ClassB implements ClassA {
private int data;
}
B. public abstract class ClassB extends ClassA {
private int data;
}
C. public class ClassB extends ClassA {
private int data;
public void quiz();
}
D. public abstract class ClassB implements ClassA {
private int data;
public void quiz();
}
E. public class ClassB extends ClassA {
private int data;
public void quiz() {/* code here */}
F. public abstract class ClassB implements ClassA {
private int data;
public void quiz() { /* code here */ }


Answer: B, E

Q: 4 Given
1. class ClassA {
2. static void questions() { /* more code here */ }
3. void answers() { /* more code here */ }
4. }
Which two statements are true? (Choose two.)

A. ClassA.answers() is a valid invocation of answers().
B. ClassA.questions() is a valid invocation of questions().
C. Method answers() can directly call method questions().
D. Method questions() can directly call method answers().


Answer: B, C

Q: 5 Given:
1. class ClassA {
2. public ClassA() { System.out.print(1); }
3. }
4. class ClassB extends ClassA {
5. public ClassB() { System.out.print(2); }
6. }
7. class ClassC extends ClassB {
8. public ClassC() { System.out.print(3); }
9. }
10. public class ClassNumbers{
11. public static void main( String[] args ) { new ClassC(); }
12. }
What is the result when this code is executed?

A. 1
B. 3
C. 123
D. 321
E. The code runs with no output.


Answer: C

Q: 6 Given:
1. class ClassA{
2. public enum Direction { NORTH, SOUTH, EAST, WEST }
3. }
4. public class ClassB{
5. // insert code here
6. }
Which code, inserted at line 5, allows the ClassB class to compile?

A. Direction data = NORTH;
B. ClassA.Direction data = NORTH;
C. Direction data = Direction.NORTH;
D. ClassA.Direction data = ClassA.Direction.NORTH;


Answer: D

Q: 7 Given:
1. class ClassA {
2. public ClassA classAObj() { return this; }
3. }
4. class ClassB extends ClassA {
5. public ClassA classAObj() { return this; }
6. }
7. class ClassC extends ClassB {
8. // insert method here
9. }
Which ClassB methods, inserted individually, correctly complete the ClassC class? (Choose ClassB.)

A. public void classAObj() {}
B. public int classAObj() { return 3; }
C. public ClassB classAObj() { return this; }
D. public ClassA classAObj() { return this; }
E. public Object classAObj() { return this; }


Answer: C, D

Q: 8 Given:
1. public static void main(String[] args) {
2. Object obj = new int[] { 1, 2, 3 };
3. int[] dataArray = (int[])obj;
4. for (int data : dataArray) System.out.print(data + " ");
5. }
What is the result?

A. 1 2 3
B. Compilation fails because of an error in line 2.
C. Compilation fails because of an error in line 3.
D. Compilation fails because of an error in line 4.
E. A ClassCastException is thrown at runtime.


Answer: A

Q: 9 Given:
1. public class ClassA implements Runnable {
2. public void run() {
3. System.out.print("Answer it yourself");
4. }
5. public static void main(String[] args) {
6. Thread t = new Thread(new ClassA());
7. t.run();
8. t.run();
9. t.start();
10. }
11. }
What is the result?

A. Compilation fails.
B. An exception is thrown at runtime.
C. The code executes and prints "Answer it yourself".
D. The code executes and prints "Answer it yourselfAnswer it yourself".
E. The code executes and prints "Answer it yourselfAnswer it yourselfAnswer it yourself".

Answer: E

Q: 10 Given:
1. public class ClassA {
2. public static void main (String[] args) throws Exception {
3. Thread.sleep(2000);
4. System.out.println("Java Quiz");
5. }
6. }
What is the result?
A. Compilation fails.
B. An exception is thrown at runtime.
C. The code executes normally and prints "Java Quiz".
D. The code executes normally, but nothing is printed.

Answer: C

Hope you found this useful! Kindly give me your comments regarding the same.Many more Quizzes are yet to be posted! Kindly make use of them as much as possible! 

Thursday, August 27, 2009

Java Quiz -11

Hi friends,

Today I would like to post few more questions to test your capability.
Dont worry guys, I will give you the answer too! Try to answer it yourself without referring to the answers to test yourself.

Q: 1 Given:
1. public class ClassA {
2. public static void main(String[] args) {
3. String myProp = /* insert code here */
4. System.out.println(myProp);
5. }
6. }
and the command line:
java -Dprop.custom=apple ClassA
Which two, placed on line 3, will produce the output apple? (Choose two.)
A. System.getProperty("prop.custom");
B. System.property("prop.custom");
C. System.getProperties().getProperty("prop.custom");
D. System.load("prop.custom");
E. System.getenv("prop.custom");

Answer: A, C

Q: 2 Given:
1. class ClassA {
2. ClassB classBobj;
3. public ClassA() { booch = new ClassB(this); }
4. }
5.
6. class ClassB {
7. ClassA classAobj;
8. public ClassB( ClassA s) { classAobj = s; }
9. }
And the statements:
10. public static void main(String[] args) {
11. ClassA classAmainObj = new ClassA();
12. classAmainObj = null;
13. // more code here
14. }
Which statement is true about the objects referenced by classAmainObj, classAobj, and classBobj immediately after line
12 executes?
 
A. Only the object referenced by classAobj is eligible for garbage collection.
B. None of these objects are eligible for garbage collection.
C. The objects referenced by classAobj and classBobj are eligible for garbage collection.
D. Only the object referenced by classBobj is eligible for garbage collection.
E. Only the object referenced by classAmainObj is eligible for garbage collection.

Answer: E

Q: 3 Given:
1. public class ClassA {
2.
3. static public void main(String [] args) {
4. for(int x = 1; x < args.length; x++) {
5. System.out.print(args[x] + " ");
6. }
7. }
8. }
and the command line invocation:
java ClassA a b c
What is the result?

A. a b
B. b c
C. a b c
D. Compilation fails.
E. An exception is thrown at runtime.

Answer: B

Q: 4 A developer is creating a class ClassA, that needs to access class ClassB. The
ClassB class is deployed in a JAR named quiz.jar. Which three, taken independently, will allow the developer to use the ClassB class while compiling the ClassA class? (Choose three.)

A. The JAR file is located at $JAVA_HOME/jre/classes/quiz.jar.
B. The JAR file is located at $JAVA_HOME/jre/lib/ext/quiz.jar.
C. The JAR file is located at /foo/quiz.jar and a classpath environment variable is set that includes
/foo/quiz.jar/ClassB.class.
D. The JAR file is located at /foo/quiz.jar and a classpath environment variable is set that includes
/foo/quiz.jar.
E. The JAR file is located at /foo/quiz.jar and the ClassA class is compiled using javac -cp /foo/quiz.jar/ClassB
ClassA.java.
F. The JAR file is located at /foo/quiz.jar and the ClassA class is compiled using javac -d /foo/quiz.jar
ClassA.java
G. The JAR file is located at /foo/quiz.jar and the ClassA class is compiled using javac -classpath
/foo/quiz.jar ClassA.java

Answer: B, D, G

Q: 5 Given:
enum Example { Questions,Answers,Test }
Which statement is true?
A. The expressions (Questions == Questions) and Questions.equals(Questions) are both guaranteed to be true.
B. The expression (Questions < Answers) is guaranteed to be true and Questions.compareTo(Answers) is guaranteed to be less
than Questions.
C. The Example values cannot be used in a raw java.util.HashMap; instead, the programmer must use a
java.util.EnumMap.
D. The Example values can be used in a java.util.SortedSet, but the set will NOT be sorted because enumerated types do NOT implement java.lang.Comparable.

Answer: A


Q: 6 Given:
1. import java.util.*;
2. public class ClassA {
3. public static void main(String[] args) {
4. PriorityQueue data = new PriorityQueue();
5. data.add("Test");
6. data.add("Questions");
7. data.add("Answers");
8. System.out.println(data.poll() + ":" + data.peek());
9. }
10. }
What is the result?
A. Questions:Questions
B. Test:Questions
C. Questions:Answers
D. Answers:Questions
E. Test:Test
F. Test:Answers


Answer: C

Q: 7 Given:

1. Object [] data = {
2. new Integer(12),
3. new String("foo"),
4. new Integer(5),
5. new Boolean(true)
6. };
7. Arrays.sort(data);
8. for(int i=0; i9. System.out.print(data[i].toString());
10. System.out.print(" ");
11. }
What is the result?
A. Compilation fails due to an error in line 1.
B. Compilation fails due to an error in line 7.
C. A ClassCastException occurs in line 7.
D. A ClassCastException occurs in line 5.
E. The value of all four objects will be printed.


Answer: C

Q: 8 Given:
1. import java.util.*;
2. public class ClassA {
3. private String s;
4. public ClassA(String s) { this.s = s; }
5. public static void main(String[] args) {
6. HashSet test = new HashSet();
7. ClassA classAobj1 = new ClassA("aardvark");
8. ClassA classAobj2 = new ClassA("aardvark");
9. String s1 = new String("aardvark");
10. String s2 = new String("aardvark");
11. test.add(classAobj1); test.add(classAobj2); test.add(s1); test.add(s2);
12. System.out.println(test.size()); } }
What is the result?
A. 0
B. 1
C. 2
D. 3
E. 4
F. Compilation fails.
G. An exception is thrown at runtime.

Answer: D  

Q: 9 Given:
1. public class ClassA {
2. private long data1;
3. private long data2;
4.
5. // class ClassA methods
6. }
A programmer is developing a class ClassA, that will be used as a ClassA in a standard java.util.HashMap.
Which two methods should be overridden to assure that ClassA works correctly as a ClassA? (Choose two.)

A. public int hashCode()
B. public boolean equals(ClassA k)
C. public int compareTo(Object o)
D. public boolean equals(Object o)
E. public boolean compareTo(ClassA k)


Answer: A, D

Q: 10 Given a pre-generics implementation of a method:
1. public static int data(List list) {
2. int data = 0;
3. for ( Iterator iter = list.iterator(); iter.hasNext(); ) {
4. int i = ((Integer)iter.next()).intValue();
5. data += i;
6. }
7. return data;
8. }
Which three changes must be made to the method data to use generics? (Choose three.)

 
A. remove line 4
B. replace line 4 with "int i = iter.next();"
C. replace line 3 with "for (int i : intList) {"
D. replace line 3 with "for (Iterator iter : intList) {"
E. replace the method declaration with "data(List intList)"
F. replace the method declaration with "data(List intList)"

 Answer: A, C, F

Hope you found this useful! Kindly give me your comments regarding the same.Many more Quizzes are yet to be posted! Kindly make use of them as much as possible!
 

Wednesday, August 26, 2009

Few Points Related To Design Patterns

Hi friends,

Its been sometime since we shared some information regarding the concepts(For those who missed out, We had been doing Few Java Quizzes! Check It out Here
http://helptotheneeded.blogspot.com/search/label/Java%20Quiz)


Today I would like to present a general hint which would be helpful for people who want to take seminars in Design Patterns.

Without wasting further time, Here We Go!

Before Design Pattern:

Interface – Car and TV remote
Abstract Class – I10
Is a - has a
Aggregation - If the History-Class object is destroyed, the Student objects may continue to exist.
History-Class << --->Student
(Weak relationship)
Composition - If the House object is destroyed, the Room objects will also be destroyed.
(Strong relationship) House << --- > Room

Architecture – Skeletal view
Framework – Implementation

Organizing the patterns:

Creational – Involve object instantiation and all provide a way to decouple a client from the objects
it needs to instantiate.
Behavioral – It is concerned with how classes and objects interact and distribute responsibility.
Structural – It lets you compose classes or objects into larger structures.

GOF – Gang of four (Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides) author’s of the book

There are 250 patterns in oo world where 23 are familiar and well known patterns.

Creational:
Singleton:

“Ensure a class has one instance and provide a global point of access to it”
public class single {
private static single s;
private single() { };

public static synchronized single getInstance()
{
if(s==null)
s = new single();
return s;
}
//other methods continue here

Factory Method:

Based on your data, we are going to instaniate an object of a classes
eg:
Car<- Buick, Chevrolet, Ford, Pontiac and Saturn
public class OrderCars
{
public Car orderCar(String model)
{
Car car;
if(model.equals("Lucerne"))
car = new Buick(model);
else if(model.equals("Corvette"))
car = new Chevrolet(model);
else if(model.equals("Fusion"))
car = new Ford(model);
else if(model.equals("GTO"))
car = new Pontiac(model);
else if(model.equals("Vue"))
car = new Saturn(model);
}
}
When a class does not know which class of objects it must create.

Abstract Factory:

Its a super set of Factory method pattern


public class ComputerType {
private Computer comp;
public static void main(String[] args) {
ComputerType type = new ComputerType();
Computer computer = type.getComputer("Server");
System.out.println("Monitor: "+computer.getMonitor().getSpecification());
System.out.println("RAM: "+computer.getRAM().getSpecification());
System.out.println("Processor: "+computer.getProcessor().getSpecification());
}
/**
* Returns a computer for a type
*
* @param computerType String, PC / Workstation / Server
* @return Computer
*/
public Computer getComputer(String computerType) { if (computerType.equals("PC"))
comp = new PC();
else if(computerType.equals("Workstation"))
comp = new Workstation();
else if(computerType.equals("Server"))
comp = new Server();
return comp;
} }// End of class

Builder and Prototype

Behavioral:

Strategy - It desines a set of algorithm and make them interchangable based on client request (refer ppt )
Observer - Refer ppt It defines a one-to-many dependency between objects so that when one object change state,
all of its dependents are notified and updated automatically (refer ppt)
Chain of Responsibility , Command , Interpreter , Iterator , Mediator, Memento

Structural:

Adapter: It converts the interface of a class into another interface the client expects. Adapter lets
classes work together that couldn’t otherwise because of incompatible interfaces (refer ppt )
Façade: This is nothing but the outlook of an object.It hides the complexity of our implementation
(refer ppt ).

Bridge ,Composite, Decorator, Façade, Flyweight and Proxy

This Information As Said Earlier is only a general hint which would be helpful for people who want to take seminars.
Hope It was useful to you, Please Post your comments!

Tuesday, August 25, 2009

Java Quiz -10

Hi friends,

Today I would like to post few more questions to test your capability.
Dont worry guys, I will give you the answer too! Try to answer it yourself without referring to the answers to test yourself.

Q: 1 Given:
1.public class TestString3{
2. public static void main(String[] args) {
3.// insert code here
4.System.out.println(s);
5.}
6.} Which two code fragments, inserted
independently at line 3, generate the output 4247? (Choose two.)

A. StringBuffer s = new StringBuffer("123456789");
s.substring(3,6).delete(1,3).insert(1, "24");
B. StringBuffer s = new StringBuffer("123456789");
s.delete(0,3).replace(1,3,"24").delete(4,6);
C. String s = "123456789";
s = (s-"123").replace(1,3,"24") - "89";
D. StringBuilder s = new StringBuilder("123456789");
s.substring(3,6).delete(1,2).insert(1, "24");
E. StringBuilder s = new StringBuilder("123456789");
s.delete(0,3).delete(1,3).delete(2,5).insert(1, "24");

Answer: B, E

Q: 2 When comparing java.io.BufferedWriter to java.io.FileWriter, which
capability exists as a method in only one of the two?

A. writing a line separator to the stream
B. writing to the stream
C. marking a location in the stream
D. flushing the stream
E. closing the stream

Answer: A

Q: 3 Given:
12. Date date = new Date();
13. df.setLocale(Locale.ITALY);
14. String s = df.format(date);
The variable df is an object of type DateFormat that has been initialized in line 11.
What is the result if this code is run on December 14, 2000?

A. The value of s is Dec 14, 2000.
B. The value of s is 14-dec-2004.
C. Compilation fails because of an error in line 13.
D. An exception is thrown at runtime.

Answer: C

Q: 4 Which three statements concerning the use of the java.io.Serializable
interface are true? (Choose three.)

A. An object serialized on one JVM can be successfully deserialized on a different JVM.
B. The values in fields with the transient modifier will NOT survive serialization and deserialization.
C. It is legal to serialize an object of a type that has a supertype that does NOT implement java.io.Serializable.
D. Objects from classes that use aggregation cannot be serialized.
E. The values in fields with the volatile modifier will NOT survive serialization and deserialization.

Answer: A, B, C

Q: 5 Given:
11. String test = "This is a test";
12. String[] tokens = test.split("\s");
13. System.out.println(tokens.length);
What is the result?

A. 10
B. 2
C. 5
D. Compilation fails.
E. An exception is thrown at runtime.

Answer: D

Q: 6 Given:
d is a valid, non-null Date object
df is a valid, non-null DateFormat object set to the current locale
What outputs the current locale's country name and the appropriate version of d's date?

A. Locale loc = Locale.getDefault();
System.out.println(loc.getDisplayCountry()
+ " " + df.format(d));
B. Locale loc = Locale.getLocale();
System.out.println(loc.getDisplayCountry()
+ " " + df.setDateFormat(d));
C. Locale loc = Locale.getDefault();
System.out.println(loc.getDisplayCountry()
+ " " + df.setDateFormat(d));
D. Locale loc = Locale.getLocale();
System.out.println(loc.getDisplayCountry()
+ " " + df.format(d));

Answer: A

Q: 7 Given classes defined in two different files:
1. package util;
2. public class ClassA {
3. private static void process(byte[] b) {}
4. }
1. package app;
2. public class ClassB {
3. public static void main(String[] args) {
4. byte[] byteobj = new byte[256];
5. // insert code here
6. }
7. }
What is required at line 5 in class SomeApp to use the process method of ClassB?

A. process(byteobj);
B. ClassB.process(byteobj);
C. app.ClassB.process(byteobj);
D. util.ClassB.process(byteobj);
E. import util.ClassB.*; process(byteobj);
F. SomeApp cannot use the process method in ClassB.

Answer: F

Q: 8 Given:
1. rbo = new ReallyBigObject();
2. // more code here
3. rbo = null;
4. /* insert code here */
Which statement should be placed at line 4 to suggest that the virtual machine expend effort toward recycling the memory used by the object rbo?

A. Runtime.getRuntime().growHeap();
B. System.freeMemory();
C. Runtime.getRuntime().freeMemory();
D. System.gc();
E. Runtime.gc();

Answer: D

Q: 9 Given classes defined in two different files:
1. package util;
2. public class ClassB {
3. public static void process(byte[]) { /* more code here */ }
4. }
1. package app;
2. public class ClassA {
3. public static void main(String[] args) {
4. byte[] byteobj = new byte[256];
5. // insert code here
6. }
7. }
What is required at line 5 in class ClassA to use the process method of ClassB?

A. ClassB.process(byteobj);
B. util.ClassB.process(byteobj);
C. process(byteobj);
D. import util.ClassB.*; process(byteobj);
E. SomeApp cannot use methods in ClassB.

Answer: B

Q: 10 Given:
11. public static void test(String string) {
12. int value = 4;
13. if ( value = string.length()) {
14. System.out.print(string .charAt( value -= 1) +", ");
15. } else {
16. System.out.print(string .charAt(0) + ", ");
17. }
18. }
and the invocation:
21. test("four");
22. test("tee");
23. test("to");
What is the result?

A. r, e, o,
B. r, t, t,
C. Compilation fails.
D. An exception is thrown at runtime.

Answer: C

Hope you found this useful! Kindly give me your comments regarding the same.Many more Quizzes are yet to be posted! Kindly make use of them as much as possible!

Monday, August 24, 2009

Java Quiz-9

Hi friends,

Today I would like to post few more questions to test your capability.
Dont worry guys, I will give you the answer too! Try to answer it yourself without referring to the answers to test yourself.

1)What is the output of following code

class MyOuter
{
private int x = 7;

public void makeInner()
{
MyInner in = new MyInner();
in.seeOuter();
System.out.println(x);
}

class MyInner
{
public void seeOuter()
{
x += (++x + x++);
System.out.println(x++);
}
}

public static void main(String [] args)
{
MyOuter outer = new MyOuter();
outer.makeInner();
}
}

Ans

23
24

2)Write code to fill up the blanks

class MyOuter
{
private int x = 7;

public void makeInner() {
MyInner in = new MyInner();
in.seeOuter();
}

class MyInner
{
public void seeOuter()
{
System.out.println("Outer x is " + x);
}
}

public static void main(String[] args)
{
MyOuter mo = new MyOuter();
_______ inner = ___________;
inner.seeOuter();
}


}

Ans

MyOuter.MyInner
mo.new MyInner()

3)What is the output of the following code?

class Popcorn
{
public void pop()
{
System.out.println("popcorn");
}
}

class Food
{
Popcorn p = new Popcorn()
{
public void sizzle ()
{
System.out.println("anonymous sizzling popcorn");
}
public void pop()
{
System.out.println("anonymous popcorn");
}
};

public void popIt()
{
p.pop();
p.sizzle();
}
}

Ans

-----
p.sizzle() will result in compilation error: cannot resolve symbol

4) Which are true about a static nested class? (Choose all that apply.)

A. You must have a reference to an instance of the enclosing class in order
to instantiate it.

B. It does not have access to non-static members of the enclosing class.

C. Its variables and methods must be static.

D. If the outer class is named MyOuter, and the nested class is named
MyInner, it can be instantiated using new MyOuter.MyInner();.

E. It must extend the enclosing class.


Ans

B and D

5)What is the problem with the following code?

class FooRunnable implements Runnable
{
public void run()
{
for(int x =1; x < 6; x++)
{
System.out.println("Runnable running");
}
}
}

public class TestThreads
{
public static void main (String [] args)
{
FooRunnable r = new FooRunnable();
r.run();
}
}

Ans

We are not creating Threads here. The programs runs single-threaded.

6)What should you do to make the run() method in the following code
to be thread-safe?

class NameRunnable implements Runnable
{
public void run()
{
for (int x = 1; x <= 3; x++)
{
System.out.println("Run by "
+ Thread.currentThread().getName()
+ ", x is " + x);
}
}
}

Ans

Nothing. The method is already thread-safe.

Hope you found this useful! Kindly give me your comments regarding the same.Many more Quizzes are yet to be posted! Kindly make use of them as much as possible!

Friday, August 21, 2009

Java Quiz -8

Hi friends,

Today I would like to post few more questions to test your capability.
Dont worry guys, I will give you the answer too! Try to answer it yourself without referring to the answers to test yourself.

1)I have class called GlobalCache in package com.planetsoft.cache.
I want to print the name of the class, without creating an object
of the class.


Ans: GlobalCache.class.getName()

2)What is the output of the following program?

System.out.println("value=" + 08);


Ans: Compilation Error: Octal digit out of range

3)What is the output of the following code

private void display()
{
int ret = catchMe(true);
System.out.println("ret=" + ret);

ret = catchMe(false);
System.out.println("ret=" + ret);

}
private int catchMe(boolean cond)
{

try
{
if (cond) throw new Exception("catch me");
return 1;
}
catch(Exception ex)
{
return 2;
}
finally
{
return 3;
}

}

Ans:

ret=3
ret=3


5)What is wrong in the following code? suggest a solution to fix it

public static void display()
{
final long MICROS_PER_DAY = 24 * 60 * 60 * 1000 * 1000;
final long MILLIS_PER_DAY = 24 * 60 * 60 * 1000;
System.out.println(MICROS_PER_DAY / MILLIS_PER_DAY);
}

Ans:

The problem is arithmetic overflow. Solution is to prefix the
numbers with L, to make the computation in long arithmetic.


6)Mr. Jack has a 15cm gold bar and wants to give Mr. Jill, 1 cm
of gold bar on day 1, 2cms of gold bar on day 2, and so on..., .Finally
Mr. Jack should give away 15 cms of gold bar on day 15.
Following are the rules
a)Mr. Jack can make at the most 3 cuts in the gold bar
b)Mr. Jack can take back the gold bars , given to Mr. Jill, on any
given day.

Ans:
Make 3 cuts at 1,2 and 4 cms

7)A car runs for 10,000 kms. It has one stepney(i.e, extra tyre).
If all tyres has to be used equally, what is the total no. of
kms, each tyre would have run?

Ans:
8000 kms

8)There are 3 fruit baskets - one with full of Apples, one with
full of Oranges and one with both apples and oranges. All boxes
are labelled wrongly. You cannot see what is inside the box.
Given these , you have to pick only one fruit from any one of the
box of your choice, and label the boxes correctly. How do you
solve this?

Ans:

----
Pick one fruit from Box labelled "Mixed"
If it is Apple then

Rename "Mixed" as "Apple".
Rename "Apple" as "Orange"
Rename "Orange" as "Mixed"

else If it is Orange then

Rename "Mixed" as "Orange".
Rename "Orange" as "Apple"
Rename "Apple" as "Mixed"

End if

9)From a List of String , how to get array of Strings
Note: You only have to fill up the blanks in the following code.

private static void display()
{
List l = new ArrayList();

l.add("tom");
l.add("harry");
String [] arr = ------------------;

}

Ans:
(String [])l.toArray(new String[0])


10)What is the output of the following method

private static void display()
{
char x = 'X';
int i = 0;
System.out.print(true ? x : 0);
System.out.print(false ? i : x);

}

Ans:
X88

Hope you found this useful! Kindly give me your comments regarding the same.Many more Quizzes are yet to be posted! Kindly make use of them as much as possible!