Sunday, March 4, 2012

Nokia Written Test Question

What is the output of following code:

public class ThreadTest1 implements Runnable{

    public static void main(String args[])throws Exception{

        Thread t1 = new Thread(new ThreadTest1());

        t1.start();
        System.out.println("Begin");
        t1.join();
        System.out.println("End");
    }

    public void run(){
        System.out.println("Run");
    }
}

Nokia Interview Written Test and Questions

Class C extends B and B extends A and how can I directly access a method of A from class C without creating an instance of A.

Options:

1. super.test()
2. super.super.test()
3. There is no way we can access the class A method without creating an instance from class C.

Answer is 3

Here is the example code (they are not given this code, I only wrote for understanding clearly)



1. class A{
    public A(){
        System.out.println("A");   
    }
    public void test(){
        System.out.println("test method of A");
    }

}
class B extends A{
    public B(){
        System.out.println("B");   
    }
    public void test(){
        System.out.println("test method of B");
    }

}

class C extends B{
    public C(){
               System.out.println("C");   
    }

    public void test(){
        System.out.println("test method of C");
    }


}


public class SuperKeyWord{
    public static void main(String args[]){
        C c = new C();
    }
}


Question 2:
Write an immutable java class example.

Question 3:
Write a java program to reverse a given string.

Question 4:
Choose all right answers from the below options.

     a. public synchronized(this) void test(){}
     b. public void test(){ synchronized(Test.class){}}
     c. public void test(){ synchronized(a) {}}
     d. public synchronized void test {}
     e. public void test{ synchronized(this) {}}

EMC Interview Question


package com.example.time.complexity;
/*Question:
 * An integer array of 5 length has some values, while traversing
 * the array multiply all other(except the current element's value)
 * array elements values and place the result  in the current array
 * element
 *
 *    ex: a[] = {11,23,35,41,2}
 *    first traverse a[0]=2x3x4x5
 *    second traverse a[1]=1x3x4x5
 *    third traverse a[2]=1x2x4x5
 *    fourth traverse a[3]=1x2x3x5
 *    fifth traverse a[4]=1x2x3x4
 */
public class ComplexityTest1 {

    public static void main(String args[]){
        int[] i = {11,23,35,41,2};
        int[] res = new int[i.length];
        int len =i.length;
        int temp=1;
        for(int x=0;x<len;x++){
            if(x+1==len){
                temp=1;
            }else{
                temp=i[x+1];
            }
               
            for(int y=0;y<len;y++){
                if(x!=y && (x+1)!=y){
                    //System.out.println(x+"."+temp+"x"+i[y]+"="+(temp*i[y]));
                    temp = temp*i[y];
                }
            }
            res[x]=temp;
        }
        for(int x=0;x<len;x++){
            System.out.println(res[x]);
        }
    }
}

Wednesday, October 26, 2011

Call by reference program in java

package org.learn.callbyreference;

public class CallByReferenceTest {

    String val = new String("Test");

    public static void main(String[] args) {
        CallByReferenceTest cTest = new CallByReferenceTest();
       
         String s= new String("test this string");
         cTest.changeString(s);
         System.out.println(s);
        

        System.out.println(cTest.val);
        cTest.chagngeVal("Test1111");
        System.out.println(cTest.val);

    }

    public void changeString(String s) {
        s = "test 89898989";
    }

    public void chagngeVal(String s) {
        val = "Test43323243242";
    }

}

Java program on Command design pattern


Save each class in a separate file with the class name and execute the Main.java program. Some classes are not used in the main execution. In later versions of this design I will include them.

----------------------------------------------------------------------------------------------------------
package org.learn.designpatterns.command;

//This class(Main.java) is client part of command design pattern
public class Main {

    public static void main(String args[]) {
        CommandManager mgr = new CommandManager();
        // CommandManager is Invoker part of command design pattern
        Command tomcat = new TomcatStartCommand();
        // In TomcatStartCommand class I used Tomcat class. Here Tomcat class is
        // the receiver part of command design pattern
        Command serviceMix = new ServiceMixStartCommand();
        mgr.addCommand(tomcat);
        mgr.addCommand(serviceMix);
        mgr.runCommands();
    }

}
-----------------------------------------------------------------------------------------------------------
package org.learn.designpatterns.command;

public interface Command {
   
    public void execute();

}
-----------------------------------------------------------------------------------------------------------
package org.learn.designpatterns.command;

public class CommandException extends Exception {
   
    private String message;
   
    public CommandException(String message){
        super(message);
        this.message=message;
    }
   
    public CommandException(){
        super();
    }
   
    public void setMessage(String message){
        this.message = message;
    }
   
    public String getMessage(){
        return message;
    }
   
}
-----------------------------------------------------------------------------------------------------------
package org.learn.designpatterns.command;

import java.util.ArrayList;

public class CommandManager {

    private List<Command> commandList = new ArrayList();

    public void runCommands() {
        for (Command c : commandList) {
            c.execute();
        }
    }

    public void addCommand(Command command) {
        commandList.add(command);
    }
}

class TomcatStartCommand implements Command {

    Tomcat tomcat = new Tomcat();

    @Override
    public void execute() {
        tomcat.startTomcat();
    }
}

class TomcatStopCommand implements Command {

    Tomcat tomcat = new Tomcat();

    @Override
    public void execute() {
        tomcat.stopTomcat();
    }
}

class ServiceMixStopCommand implements Command {
    ServiceMix serviceMix = new ServiceMix();

    @Override
    public void execute() {
        serviceMix.stopServiceMix();
    }

}

class ServiceMixStartCommand implements Command {
    ServiceMix serviceMix = new ServiceMix();

    @Override
    public void execute() {
        serviceMix.startServiceMix();
    }

}
----------------------------------------------------------------------------------------------------------


package org.learn.designpatterns.command;

public class ServiceMix {
   
    public boolean stopServiceMix(){
        System.out.println("Service Mix is stopped");
        return true;
    }
   
    public boolean startServiceMix(){
        System.out.println("Service Mix is started");
        return true;
    }

}
------------------------------------------------------------------------------------------------------------
package org.learn.designpatterns.command;

public class Status {
    private String status;

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }
   
   
}
------------------------------------------------------------------------------------------------------------
package org.learn.designpatterns.command;

public class Tomcat {
   
    public boolean stopTomcat(){
        System.out.println("Tomcat Stopped");
        return true;
    }
   
    public boolean startTomcat(){
        System.out.println("Tomcat Started");
        return true;
    }

}
------------------------------------------------------------------------------------------------------------

Simple Binary Tree implementaion in java

package org.learn.datastructures.tree.binary;

public class BinaryTree {

    /**
     * @param args
     */
    public static void main(String[] args) {
        BinaryTree tree = new BinaryTree();
        Node rootNode = new Node(10);
        tree.insert(rootNode, 20);
        tree.insert(rootNode, 30);
        tree.insert(rootNode, 5);
        tree.insert(rootNode, 6);
        tree.insert(rootNode, 16);
        tree.insert(rootNode, 67);
        tree.insert(rootNode, 116);
        tree.search(rootNode);
    }

    public void insert(Node node, int data) {
        if (node.data < data) {
            if (node.left != null)
                insert(node.left, data);
            else
                node.left = new Node(data);
        } else if (node.data > data) {
            if (node.right != null)
                insert(node.right, data);
            else
                node.right = new Node(data);
        }
    }

    public void search(Node node) {
        if (node == null)
            return;
        search(node.right);
        System.out.println(node.data);
        search(node.left);
    }

}

class Node {
    Node left;
    Node right;
    int data;

    public Node(int data) {
        this.data = data;
    }
}

A simple compression program in java

package org.learn.compression;

public class Compression {

    /**
     * @param args
     */
    public static void main(String[] args) {
        String toBeCompressed = "aaaaaabbbbbcccce4uiiiioookkkkkkkkkkkkkkkkkkkkkkkk";
        String compressed = compress(toBeCompressed);
       
        System.out.println("String to Compressed="+toBeCompressed + ", length="+toBeCompressed.length());
        System.out.println("Compressed String ="+compressed+ ", length="+compressed.length());
    }
   
    public static String compress(String str){
        String compressedStr="";
        String countChar="";
        int index=0;
        int strLength = str.length();
        while(true){
            char c = str.charAt(index);
            int count=1;
            int temp=index;
            while(true){
                countChar="";
                ++temp;
                if( temp<strLength && c == str.charAt(temp)){
                    count++;
                }else{
                    countChar = c + ""+count;
                    break;
                }
            }
            compressedStr = compressedStr + countChar;
            index+=count;
            if(index>=strLength)
                break;
        }
        return compressedStr;
    }

}