It2EDU

Thursday, April 13, 2017


Dear all Gate aspirants in this blog, I shared GATE previous exam papers.



Computer science paper1 download here and answer paper 

Download here



Computer science paper2 Download here and answer paper Download here



Sunday, March 5, 2017


Java Strings



Strings are widely used in Java programming. A string is a sequence of characters or simply says a character array. Strings are treated as Objects.
The Java platform provides the String class to create and manipulate strings.




How to create a String:




The most direct way to create a string is to write:

String greeting = "Hello world!";

Whenever it encounters a string literal in your code, the compiler creates a String object with its value in this case,

"Hello world!'.

As with any other object, you can create String objects by using the new keyword and a constructor.

The String class has eleven constructors that allow you to provide the initial value of the string using different sources, such as an array of characters.



public class StringDemo{


public static void main(String args[]){

char[] helloArray = { 'h', 'e', 'l', 'l', 'o', '.'};

String helloString = new String(helloArray);

System.out.println(helloString );

}

}

The above code snippet gives the output hello





“The String class is immutable, so that once it is created a String object cannot be changed. The String class has a number of methods, some of which will be discussed below, that appear to modify strings. Since strings are immutable, what these methods really do is create and return a new string that contains the result of the operation”



Methods used to obtain information about an object are known as accessor methods. One accessor method that you can use with strings is the length() method, which returns the number of characters contained in the string object.


After the following two lines of code have been executed, len equals 17:



String palindrome = "Dot saw I was Tod";

int len = palindrome.length();



String Concatenation



Concatenating means combine two strings one at the end of the other.

In strings concat(String str) is a method used to concatenate the strings.

The String class includes a method for concatenating two strings:


string1.concat(string2);


This returns a new string that is string1 with string2 added to it at the end.


You can also use the concat() method with string literals, as in:


"My name is ".concat("Rumplestiltskin");


Strings are more commonly concatenated with the + operator, as in


"Hello," + " world" + "!"


which results in


"Hello, world!"


The + operator is widely used in print statements. For example:


String string1 = "saw I was ";

System.out.println("Dot " + string1 + "Tod");


which prints


Dot saw I was Tod


Such a concatenation can be a mixture of any objects. For each object that is not a String, its toString() method is called to convert it to a String.





Thursday, February 16, 2017


Java Interview Questions



1. Can we write a java program without main() method?

Ans: Yes we can write java program without main() method.

public class WithoutMain{

static{

System.out.println("without main method ");

System.exit(0);

}

}





2.What is the super class of every java class?


Ans: Object is the super class of every java class.





3.What is the difference between an Interface and an Abstract class?

Ans: An abstract class can have abstract and non-abstract methods but interface can have only abstract methods.

The abstract is the keyword is used to declare abstract class.

The interface is keyword used to declare interface.

Abstract classes does not support multiple inheritance. Interface supports multiple inheritance.

An abstract class can have static methods,constructor and main method. but interface cannot have these.

abstract classes can provide implementation for interfaces but interfaces cannot provide implementation of abstract classes.







Tuesday, January 3, 2017









Bucket sort is a sorting algorithm also knows as bin sort,
it works with distributing elements of an array into a number of buckets. 





A bucket is most commonly a type of data buffer or a type of
document in which data is divided into regions. Elements or contents in the
bucket are unsorted and size of the bucket is fixed at the time of creation.


A bucket has states of empty, non-empty, full or partly
full, and overflows.





Each bucket is sorted individually, using any sorting
algorithm or bucket sorting.  Basically
bucket sort is distribution sort and it is cousin of radix sort. Bucket sort
can be implemented with comparisons and therefore can also be considered a
comparison sort algorithm. 





Worst – case performance:      O(n2)


Best Case Performance:  
        Ω(n+k)


Average Performance:            
 
θ(n+k)


Worst case space complexity:  O(n.k)


  


Pseudo code of Bucket sort:


 








function
bucketSort(array, n) is


  buckets ← new array of n empty lists


  for i = 0 to (length(array)-1) do


    insert array[i] into
buckets[msbits(array[i], k)]


  for i = 0 to n - 1 do


    nextSort(buckets[i]);


  return the concatenation of
buckets[0], ...., buckets[n-1]







Please look at the instance:


 


Given Series is: [34,12,45,23,1,3,4,36,19,20,28,56,67,48,59]





Distribute these elements into buckets/bins like below





 




Bucket Sort
Distribution of given Elements in Buckets


 In the above picture elements are distributed among the
bins/buckets







Bucket sort
Sorting in Each Bin


Elements are sorted in each bin/bucket.





Implement bucket sort in java:




import java.util.*;









public class BucketSort{









   public static void
sort(int[] a, int limit) {




      int [] bucket=new
int[limit+1];









      for (int i=0;
i<bucket.length; i++) {




         bucket[i]=0;




      }









      for (int i=0;
i<a.length; i++) {




        
bucket[a[i]]++;




      }









      int outPos=0;




      for (int i=0;
i<bucket.length; i++) {




         for (int j=0;
j<bucket[i]; j++) {




           
a[outPos++]=i;




         }




      }




   } 


   public static void
main(String[] args) {




      int limit=5;




      int [] data=
{5,3,0,2,4,1,0,5,2,3,1,4};









     
System.out.println("Before: " + Arrays.toString(data));




      sort(data,limit);




     
System.out.println("After: 
" + Arrays.toString(data));




   }




}











Out put of the above program is:










Sunday, December 25, 2016


Java Program - Basic Syntax:




public class ClassName {


          public static void main(String args[]){


         Variables;

    
         Statements;


    }

}



First Java Program:










public class FirstJavaProgram
{





   /* This is my first java program. 


    * This will print 'Hello World' as the
output


    */


      public static void main(String []args) {





      
System.out.println("Welcome to java world"); // prints welcome
to java World





    }


}  





First type the above code in Note pad and save with name "FirstJavaProgram.java" .





Compilation of Java program:













C:\>mydisk>javac FirstJavaProgram.java





Run the above program.





C:\>mydisk>java FirstJavaProgram







Out put is:





Welcome to java world.





Have a look at the Public static void main().





 Publicmain() method is the first method called by java environment when a program is executed so it has to accessible to from the java environment. Hence the access specifier is Public.





static:  Java environment should be able to call this method without creating an instance of the class.so this method must be declared as static.





void: Void does not return any thing so main() method is void.