Saturday, January 29, 2011

Core Java-Sample questions-Part 3

11.
package practice;

public class Tester {
       void display(){
              int arr[][]=new int[][5];
       }
       public static void main(String []args){
              Tester t=new Tester();
              t.display();
       }
}
Ans: Compilation Error
Explanation: In the line int arr[][]=new int[][5], the second dimension cannot be specified after an empty array dimension.

12.
package tpg;

public class Tester {
    public static void main(String []args){
        Tester t=new Tester();
        t.display();
    }

    public void display(){
        int arr[][];
    }
}

Ans: No error.
Explanation: It is perfectly legal to declare an array without declaring its size.

13.

package tpg;

public class Tester {
    public static void main(String []args){
        Tester t=new Tester();
        t.display();
    }

    public void display(){
        int arr[]=new int[5];
        for(int i=0;i<arr.length;i++)
        {
            System.out.println("Element value: "+arr[5]);
        }
    }
}

Ans: ArrayIndexOutOfBoundsException occurs at runtime.
Explanation: For arrays in Java, index begins at 0.

14.

package tpg;

interface A{
     void k();
}

class Tester implements A{
     void k(){
          System.out.println("Java is fun");
     }
}

Ans: Compilation error
Explanation: All methods in an interface are by default public abstract void. But while over-riding in the class Tester, we are attempting to assign weaker access privilege in class Tester(default or package level access if not mentioned). Implementation in the class Tester should be done using the word public.

15.

package tpg;

public class Tester {

    public static void main(String[] args) {
        Tester t = new Tester();
        t.display();
    }

    public void display() {
        int i = 1;
        if (i) {
            System.out.println("In IF condition");
        } else {
            System.out.println("In ELSE condition");
        }

    }
}

Ans: Compilation error
Explanation: IF condition in Java accepts only Boolean values and not integers like C Language. The logic would have worked if the program was written in C language.

Friday, January 28, 2011

Core Java-Sample questions-Part 2

6.

public class Tester {
       void display(){
              String name="Wipro";
              System.out.println("Length "+name.length);
       }
       public static void main(String []args){
              Tester t=new Tester();
              t.display();
       }
}

Ans: Compilation error
Explanation: In the line System.out.println("Length "+name.length), instead of length, length() method should be used. Length() is a method in String class while length is an attribute to find array length.

7.
package practice;

public class Tester {
       void display(){
              int[] arr[]=new int[5];
       }
       public static void main(String []args){
              Tester t=new Tester();
              t.display();
       }
}
Ans: Compilation error
Explanation: In the line int[] arr[]=new int[5], a 2-D array is declared but size allocated is wrong.


8.
public class Tester {
       void display(){
              int[] arr[]=new int[5][5];
       }
       public static void main(String []args){
              Tester t=new Tester();
              t.display();
       }
}

Ans: No error.
Explanation: int[] arr[]=new int[5][5] is also a way of declaring 2-D array.

9.

package practice;

public class Tester {
       void display(){
              int[5][5] arr=new int[][];
       }
       public static void main(String []args){
              Tester t=new Tester();
              t.display();
       }
}
Ans: Compilation error.
Explanation: Array size cannot be initialized during declaration.

10.
package practice;

public class Tester {
       void display(){
              int arr[][]=new int[5][];
       }
       public static void main(String []args){
              Tester t=new Tester();
              t.display();
       }
}

Ans: No error.
Explanation: It is perfectly legal to mention the size of first dimension and leave the other dimension empty.

Wednesday, January 26, 2011

Core Java-Sample questions-Part 1

Hi all,

I have prepared an understanding document with 50 questions on Core Java. I would be sharing the same in the next 10 posts.

Happy learning!!!

1.

package practice;

public class Tester {
       int transient=1;
       void display(){
              System.out.println(transient);
       }
}

Ans: Compilation error.
Explanation: The variable name transient is invalid as it is a keyword. Keywords cannot be used to declare variables in Java.

2.
package practice;

public class Tester {
       void display(){
              System.out.println("Output: "+'1'+'2'+"Wipro");
       }
       public static void main(String []args){
              Tester t=new Tester();
              t.display();
       }
}

Ans: 12Wipro
Explanation: ‘+’ is a concatenation operator in this example.

3.
package practice;

public class Tester {
       void display(){
              String sample='1';
              System.out.println("Output: "+'1'+'2'+"Wipro");
       }
       public static void main(String []args){
              Tester t=new Tester();
              t.display();
       }
}


Ans: Compilation Error
Explanation: In the line String sample=’1’, type mismatch occurs. You cannot store a character in a variable declared as String type.

4.
public class Tester {
       void display(){
              char ch='1';
              System.out.println("Value: "+ch);
             
       }
       public static void main(String []args){
              Tester t=new Tester();
              t.display();
       }
}
Ans: 1
Explanation: N/A

5.
package practice;

public class Tester {
       void display(){
              int arr[]={'1','2','3','4','5'};
              for(int count=0;count<arr.length();count++){
                     System.out.println("Value: "+arr[count]);
              }
       }
       public static void main(String []args){
              Tester t=new Tester();
              t.display();
       }
}
Ans: Compilation error
Explanation: length is an attribute used to find the size of array, but length() is an method in String class. The program would have compiled successfully if length was used instead of length() method.

Tuesday, January 25, 2011

Spring 3.0 Model View Controller-Working



Spring Model View Architecture. Third party image.
Hi friends,

Topic for today is the Spring 3.0 MVC !!

The best way to learn something is to associate it with what you like the most!

My most favourite dish is Chilli Parotta! I believe that there is no better example than that which can be used here to learn Spring 3.0 MVC !!! Unfortunately, it may not be your favourite dish, but you can imagine your own dish as you read the lines.

Before tasting the spiciness of this dish, let's learn some technical jargons and let's make sure that those terms and definitions are burnt and roasted on ours brains very well.

Purpose of Spring MVC:
Why applications are rewritten using frameworks?
* To isolate different elements of a software.
* Facilitates loose-coupling and creates a more of configuration type environment.
* To support more functionalities like Internationalization and Localization.
What is MVC mean?
MVC stands for Model-View-Controller.

* Model in 'MVC' represents data and business logic of the application or raw data.
* View represents a JSP or JSF which processes & displays raw data in a user-friendly format.
* Controller receives web request performs little or no processing and returns a ModelAndView object.

Note: It is to be noted that Controller and View are the inputs and outputs of the MVC architecture.

Working of Spring MVC:

* Whenever a user clicks a link and submits a form through the web browser, a request is created and it carries information like URL and even other data(form data, application data etc etc). The first stop of the request is the Dispatcher Servlet, which is like a front-end controller. Dispatcher servlet's job is to send request to Spring MVC.

* A Controller is a Spring component that processes the request forwarded by Dispatcher servlet. An application may have several controllers. So a dispatcher servlet must consult one or more handler mapper to figure out which controller is the suitable. Handler mapping would pay attention to the URL carried by the request when makings its decision. Once controller has been chosen, dispatcher servlet drops the request at the controller's place and waits till controller processes it. Controller returns a Model and View object to the dispatcher servlet.

Note: ModelAndView contains both model data and a small hint to what view should be used to render the display. So it carries only a 'logical name' of view.

* The dispatcher servlet would now ask a view resolver to help find the actual JSP so that properly formatted data is displayed to the user.
* The model data is delivered to the view and view does the remainder of the task and the job is over!

Now let's apply the same concept to prepare a Chilli Parotta or Chilli Paratha.

Imagine as follows:
You have just walked into a local restaurant in your city. A waiter or a waitress(if you are lucky) greets you with a warm smile. He is like the Dispatcher Servlet. He receives your order and passes it to the chef who is like the front-end Controller. He prepares your beloved dish and returns back the raw dish(ModelAndView) to the waiter or dispatcher servlet. Now the waiter would proceed to another chef who is like a view resolver who adds some decorations, salt, spice along with some chilli sauce and onion raitha to make the dish appear more presentable, delicious and tasty(in our context, proper formatted output for the JSP).

Hope the dish was tasty!

Advantages of Spring Framework:
* Easier to maintain code as it is more of a configuration type. A properly designed controller class will have less processing. All major processes would be handled by Service class or Data Access Object classes.
* Easier to make future enhancements as there is isolation of different elements involved in overall process. Making future enhancements if the business logic changes would be easy.

So if the waiter is not doing his service properly he can be fired and easily replaced by a better person who does good job. A better Chef could be hired and just incase if you decide to have Chilli Paratha in Madurai style than Chennai style, you need a separate view altogether as Madurai's Chilli Paratha is entirely different.

That's it for today guys, looks like everyone is laughing... Learning could be fun too...

Till we meet again next time,

Cheers from Object-ville,

Regards,
Krishna Kumar.S
New Number 1.6, Old Number 1.5,
6th Generics Lane,
Objectville.

Saturday, January 22, 2011

Hello world :-)

I am no great programmer neither am I certified in this line of work, but there are certain things which I know which would be helpful to other programmers. After all knowledge is something to receive and share. So as days go by, whenever I get time and whenever I am not lazy, I would be uploading my views, analysis, brain-teasing questions which would help Java newbies in their certification preparations and to enchance their competency. Let's make Indian Information Technology field->an industry a field without silly work-politics, where people share their knowledge and pass it on to others without any greed of thoughts.

Let's work together in such a way that everyone is mutually benefitted and everyone gains knowledge. Let's have a sense of brotherhood and help each other's career. Let's see our own victory in another person's career success.

With warm wishes from Objectville,

Krishna Kumar.S
New Number 1.6, Old Number 1.5,
5th Generics Lane,
Objectville.