Friday, October 30, 2015

Thread And Runnable both have run method which one will be called?

class MyThread extends Thread 
{ 
    MyThread() {} 
    MyThread(Runnable r) {super(r); } 
    public void run() 
    { 
        System.out.print("Inside Thread ");
    } 
} 
class MyRunnable implements Runnable 
{ 
    public void run() 
    { 
        System.out.print(" Inside Runnable"); 
    } 
} 
class Test 
{  
    public static void main(String[] args) 
    { 
        new MyThread().start(); 
        new MyThread(new MyRunnable()).start(); 
    } 
}
A.
Prints "Inside Thread Inside Thread"
B.
Prints "Inside Thread Inside Runnable"
C.
Does not compile
D.
Throws exception at runtime
Answer: Option A
Explanation:
If a Runnable object is passed to the Thread constructor, then the run method of theThread class will invoke the run method of the Runnable object.
In this case, however, the run method in the Thread class is overridden by the run method in MyThread class. Therefore the run() method in MyRunnable is never invoked.
Both times, the run() method in MyThread is invoked instead.


Reference: http://www.indiabix.com/online-test/java-programming-test/61

IllegalThreadStateException

class MyThread extends Thread 
{
    public static void main(String [] args) 
    {
        MyThread t = new MyThread();
        t.start();
        System.out.print("one. ");
        t.start();
        System.out.print("two. ");
    }
    public void run() 
    {
        System.out.print("Thread ");
    }
}
A.
Compilation fails
B.
An exception occurs at runtime.
C.
It prints "Thread one. Thread two."
D.
The output cannot be determined.
Answer: Option B

Explanation:
When the start() method is attempted a second time on a single Thread object, the method will throw an IllegalThreadStateException (you will not need to know this exception name for the exam). Even if the thread has finished running, it is still illegal to call start() again.

short circuit operator in java

class BoolArray 
{
    boolean [] b = new boolean[3];
    int count = 0;

    void set(boolean [] x, int i) 
    {
        x[i] = true;
        ++count;
    }

    public static void main(String [] args) 
    {
        BoolArray ba = new BoolArray();
        ba.set(ba.b, 0);
        ba.set(ba.b, 2);
        ba.test();
    }

    void test() 
    {
        if ( b[0] && b[1] | b[2] )
            count++;
        if ( b[1] && b[(++count - 2)] )
            count += 7;
        System.out.println("count = " + count);
    }
}


Result count = 3

Why?

The reference variables b and x both refer to the same boolean array. count is incremented for each call to the set() method, and once again when the first if test istrue. Because of the && short circuit operator, count is not incremented during the second if test.

Output of program

class SC2 
{
    public static void main(String [] args) 
    {
        SC2 s = new SC2();
        s.start();
    }

    void start() 
    {
        int a = 3;
        int b = 4;
        System.out.print(" " + 7 + 2 + " ");
        System.out.print(a + b);
        System.out.print(" " + a + b + " ");
        System.out.print(foo() + a + b + " ");
        System.out.println(a + b + foo());
    }

    String foo() 
    {
        return "foo";
    }
}


Result
72 7 34 foo34 7foo

Description:

Because all of these expressions use the + operator, there is no precedence to worry about and all of the expressions will be evaluated from left to right. If either operand being evaluated is a String, the + operator will concatenate the two operands; if both operands are numeric, the + operator will add the two operands.

Sunday, September 7, 2014

Implement a stack using one queue

Implement stack only using one queue.



 #include<stdio.h>  
 #include<stdlib.h>  
 typedef struct queue node;  
 struct queue{  
         int d;  
         node *n;          
   
 };  
 int size=0;  
 node *f,*r;  
 void enq(int d)  
 {  
         node *t;  
         t=(node *)malloc(sizeof(node));  
         t->d=d;  
         t->n=NULL;  
         if(r)  
         {  
                 r->n=t;  
                 r=t;  
         }  
         else  
         {  
                 f=r=t;  
         }  
         size++;  
 }  
 node *deq()  
 {  
         node *t;  
         if(f)  
         {  
                 size--;  
                 t=f;  
                 f=f->n;  
                 return t;  
   
         }  
         else  
         {  
                 printf("Error");  
         }  
 }  
 void push(int d)  
 {  
         enq(d);  
   
 }  
 void pop()  
 {  
         int i=0,j=size;  
         node *t;  
         while(i<j-1)  
         {  
                 t=deq();  
                 enq(t->d);  
                 i++;  
         }  
         t=deq();  
         printf("%d ",t->d);          
   
 }  
 void main()  
 {  
         push(1);  
         push(2);  
         push(3);  
         push(4);  
         push(5);  
         pop();  
         pop();  
         pop();  
         pop();  
         pop();  
   
           
 }  

Saturday, September 6, 2014

Clone a Binary Tree with Random Pointers Given a Binary Tree where every node has following structure.
struct node {
int key;
struct node *left,*right,*random;
}
The random pointer points to any random node of the binary tree and can even point to NULL, clone the given binary tree.
You can get code in c++ Here: https://gist.github.com/gk1721/36775e17719d460ac231

Wednesday, August 27, 2014

Linkage in c

 Translation unit:
  A program in a single file - single translation unit.

sharing functions and variable btw translation units : possible if variables and function have external linkage
by default global variable and non static functions (non static variables also) has external linkage so can be shared

Ex

file1.c                                                file2.c
int a=0;                                              extern int a;   //  refering to file1.c's a
void abc()                                          void ab()
{                                                         {
                                                                  abc();        //
                                                                  notshared();   // error can't access notshared
}                                                         }
static void notshared()                      
{


}

variable and functions which has internal linkage like static functions and variables can't be accessed in other files(Translation units).

No linkage :   variables within function

Linkage :
        1. External
        2. Internal
        3. No linkage