Sunday, 22 November 2015

Skilrock Technologies Interview Questions

1.Write a Program to find the frequency of a number in a two-dimesnsional array?

 import java.util.Scanner;  
 class FindFrequency {  
   public static void main(String args[]) {  
     Scanner sc = new Scanner(System.in);  
     int value[] = {  
       0, 1, 2, 3, 4, 5, 6, 7, 8, 9  
     };  
     int flag[] = new int[10];  
     System.out.println("Enter the value of n:");  
     int n = sc.nextInt();  
     int arr[][] = new int[n][n];  
     for (int i = 0; i < n; i++) {  
       for (int j = 0; j < n; j++) {  
         arr[i][j] = sc.nextInt();  
       }  
     }  
     for (int i = 0; i < n; i++) {  
       for (int j = 0; j < n; j++) {  
         Inner: for (int k = 0; k < 10; k++) {  
           if (arr[i][j] == value[k]) {  
             flag[k]++;  
             break Inner;  
           }  
         }  
       }  
     }  
     for (int k = 0; k < 10; k++)  
       System.out.println("value of" + k + "is" + flag[k]);  
   }  
 }  







2.Write a Program to find the sum of diagonal elements using one variable?

 import java.util.Scanner;  
 class Sum {  
   public static void main(String args[]) {  
     Scanner sc = new Scanner(System.in);  
     int n = sc.nextInt();  
     int arr[][] = new int[n][n];  
     for (int i = 0; i < n; i++) {  
       for (int j = 0; j < n; j++) {  
         arr[i][j] = sc.nextInt();  
       }  
     }  
     int sum = 0;  
     for (int i = 0; i < n; i++) {  
       sum = sum + arr[i][i];  
     }  
     System.out.println(sum);  
   }  
 }  

3.Tell me about your project?
   This Question is asked in each Technical Interview.Prepare these question self:-
    A.Disadvantage of old System.
    B.Advantage of new System.
    C.Requirement Specification.
    D.Process implemented in project
    E.Tables Used(Database).

4.Other Questions:-
   A:-Sudoku
   B:-bugs in any Real world applications like Facebook/Whatsapp
   C:-Second Highest salary (SQL Query)
   D:-Merging of Two binary Search tree
   E:-Manipulation of Strings(like remove kth character from a String)
 
All The Best Guys!
Keep Calm & NeverGiveUp

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
   

Wednesday, 11 November 2015

Create your Own appletviewer

//Test.java
import java.applet.Applet;
import java.awt.Graphics;
class Test extends Applet
{
public void paint(Graphics g)
{
g.drawString("rakesh",200,200);
}
}

//MyViewer.java

import java.awt.Frame;
import java.awt.Graphics;
import java.applet.Applet;
class MyViewer extends Frame
{
public static void main(String args[]) throws Exception
{
MyViewer v1=new MyViewer();
v1.setSize(400,400);
v1.setLayout(null);
v1.setVisible(true);
Class c=Class.forName(args[0]);
Applet a1=(Applet)c.newInstance();
a1.start();
Graphics g=v1.getGraphics();
a1.paint(g);
a1.stop();
}
}



Write a Program that works as javap tool

 import java.lang.reflect.*;  
 class JavaP {  
      public static void main(String args[]) {  
           try {  
                Class c = Class.forName(args[0]);  
                System.out.println("Fields.........................................");  
                Field f[] = c.getDeclaredFields();  
                for (int i = 0; i < f.length; i++)  
                     System.out.println(f[i]);  
                System.out.println("Constructors...................................");  
                Constructor t[] = c.getDeclaredConstructors();  
                for (int i = 0; i < t.length; i++)  
                     System.out.println(t[i]);  
                System.out.println("Methods........................................");  
                Method m[] = c.getDeclaredMethods();  
                for (int i = 0; i < m.length; i++)  
                     System.out.println(m[i]);  
           } catch (ClassNotFoundException e) {  
                System.out.println(e.getMessage());  
           } catch (SecurityException e) {  
                System.out.println(e.getMessage());  
           }  
      }  
 }  

Monday, 9 November 2015

Quick review of Collections

The Most Commonly used Core interfaces are:-

1.Collection 
2. Set 
3. SortedSet 
4. List 
5. Map 
6. SortedMap 
7. Queue

and the most frequently used core concrete implementation classes are

1. HashMap 
2. HashSet 
3. ArrayList 
4. PriorityQueue 
5. Collections 
6. Hashtable 
7. LinkedHashSet 
8. Vector 
9. Arrays 
10. TreeMap 
11. TreeSet 
12. LinkedList 
13. LinkedHashMap 


Remember none of the Map-related classes and interfaces extend from Collection.

So while SortedMap, Hashtable, HashMap, TreeMap, and LinkedHashMap are all thought of as collections, none are actually extended from Collection(interface).

collection (lowercase c), which represents any of the data structures in which objects are stored and iterated over.

Collection (capital C), which is actually the java.util.Collection interface from which Set, List, and Queue extend. (That's right, extend, not implement. There are no direct implementations of Collection.)

Collections (capital C and ends with s) is the java.util.Collections class that holds a pile of static utility methods for use with collections.

Collections come in four basic flavors:

1. Lists of things Ordered, duplicates allowed, with an index.

2. Sets of things May or may not be ordered and/or sorted; duplicates not allowed.

3. Maps of things with keys May or may not be ordered and/or sorted; duplicate keys are not allowed.

4. Queues of things to process Ordered by FIFO or by priority.


But there are sub-flavors within those four flavors of collections:

Sorted 
Unsorted
Ordered
Unordered

An implementation class can be unsorted and unordered, ordered but unsorted, or both ordered and sorted. 

But an implementation can never be sorted but unordered, because sorting is a specific type of ordering. 
For example, a HashSet is an unordered, unsorted set, while a LinkedHashSet is an ordered (but not sorted) set that maintains the order in which objects were inserted.

Ordered: When a collection is ordered, it means you can iterate through the collection in a specific (not-random) order. 

Sorted: Order in the collection is determined according to some rule or rules, known as the sort order. A sort order has nothing to do with when an object was added to the collection, or when was the last time it was accessed, or what "position" it was added at. Sorting is done based on properties of the objects themselves. You put objects into the collection, and the collection will figure out what order to put them in, based on the sort order. 

A collection that keeps an order (such as any List, which uses insertion order) is not really considered sorted unless it sorts using some kind of sort order. 

Most commonly, the sort order used is something called the natural order.

List Interface:

A List cares about the index. (Ordered but Not Sorted)

The one thing that List has that non-lists don't have is a set of methods related to the index. 

All three List implementations are ordered by index position—a position that you determine either by setting an object at a specific index or by adding it without specifying position, in which case the object is added to the end. 

ArrayList: Fast iteration and fast random access.

ArrayList implements RandomAccess interface—a marker interface (meaning it has no methods) that says, "this list supports fast (generally constant time) random access." 

Vector: It's like a slower ArrayList, but it has synchronized methods.

LinkedList: Elements are doubly-linked to one another. Good for adding/remove elements to the ends, i.e., stacks and queues.


Note : 
--> Choose ArrayList over a LinkedList when you need fast iteration i.e random access.
--> Go for Linked List when you need fast insertion and deletion.


Set Interface : A Set cares about uniqueness—it doesn't allow duplicates. 

HashSet: Fast access, assures no duplicates, provides no ordering.

LinkedHashSet: No duplicates; iterates by insertion order.

TreeSet: No duplicates; iterates in sorted order. Implements SortedSet.

Note : When using HashSet or LinkedHashSet, the objects you add to them must override hashCode(). If they don't override hashcode(), the default object. hashcode() method will allow multiple objects that you might consider "meaningfully equal" to be added to your "no duplicates allowed" set.

Map Interface : Cares about unique identifiers. Supports use of key/value or name/value pair. The Map implementations let you do things like search for a value based on the key, ask for a collection of just the values, or ask for a collection of just the keys. 

Like Sets, Maps rely on the equals() method to determine whether two keys are the same or different.

HashMap: unsorted, unordered Map. Fastest updates (key/value pairs); allows one null key, many null values.

Hashtable : Like a slower HashMap (as with Vector, due to its synchronized methods). No null values or null keys allowed.

LinkedHashMap : Faster iterations; iterates by insertion order or last accessed; allows one null key, many null values.

TreeMap : A sorted map. Implements SortedMap.

Note: 
LinkedList ---> faster insertions and deletions and slower iterations (compared to arraylist)
LinkedHashMap ---> faster iterations and slower insertions and deletions (compared to HashMap)

Queue Interface : Although other orders are possible, queues are typically thought of as FIFO (first-in, first-out). Queues support all of the standard Collection methods and they also add methods to add and subtract elements and review queue elements.

PriorityQueue: A to-do list ordered by the element's priority.