Facebook

adsense

Tuesday 28 April 2015

JAVA PRACTICE QUESTION

These question are really helpful to improve your java concept and learn how to handle different situation in java.These question will check your java programming skills .In your practical life when you will go for an interview these types of question are asked so these question are also helpful for these who are scared about there interview .

Actually these question cover the main java concept,how to handle different tactics/problems in java and some tricky and interesting part in java 

    -  QUESTION 1
    -  QUESTION 2 
    -  QUESTION 3
    -  QUESTION 4
    -  QUESTION 5
    -  QUESTION 6
    -  QUESTION 7
    -  QUESTION 8
    -  QUESTION 9
    -  QUESTION 10
     -  QUESTION 11
    -  QUESTION 12
    -  QUESTION 13
    -  QUESTION 14
    -  QUESTION 15
NEW:
      -  QUESTION 16
     -  QUESTION 17
    -  QUESTION 18
    -  QUESTION 19
    -  QUESTION 20

     





NOTE :
-If you want me to post more question's like that ,than please answer these question in the comment below and also give me your review about the blog.

QUESTION 10

What results from the following code?

1.    class MyClass
2.    {
3.        void myMethod(int i) {System.out.println("int version");}
4.        void myMethod(String s) {System.out.println("String version");}
5.        public static void main(String args[])
6.        {
7.            MyClass obj = new MyClass();
8.            char ch = 'c';
9.            obj.myMethod(ch);
10.        }
11.    }

-------------------------------------------------------------------------
A. Line 4 will not compile as void methods can't be overridden.
B. An exception at line 9.
C. Line 9 will not compile as there is no version of myMethod which takes a char as argument.
D. The code compiles and produces output: int version.
E. The code compiles and produces output: String version.
                   
                       <PREVIOUS  || Main Page || NEXT>
    Give your answers in the comment below

QUESTION 9

What will happen when you attempt to compile and run the following code?

class MyThread extends Thread
{
    public void run()
    {
        System.out.println("MyThread: run()");
    }
    public void start()
    {
        System.out.println("MyThread: start()");
    }
}
class MyRunnable implements Runnable
{
    public void run()
    {
        System.out.println("MyRunnable: run()");
    }
    public void start()
    {
        System.out.println("MyRunnable: start()");
    }
}
public class MyTest 
{
    public static void main(String args[])
    {
        MyThread myThread  =  new MyThread();
        MyRunnable myRunnable = new MyRunnable();
        Thread thread  =  new Thread(myRunnable);
        myThread.start();
        thread.start();
    }
}
--------------------------------------------------------------------------
A. Prints : MyThread: start() followed by MyRunnable:run()
B. Prints : MyThread: run() followed by MyRunnable:start() 
C. Prints : MyThread: start() followed by MyRunnable:start() 
D. Prints : MyThread: run() followed by MyRunnable:run() 
E. Compile time error 
F. None of the above

                   <PREVIOUS || Main Page || NEXT >
    Give your answers in the comment below

QUESTION 8

What will be the result of executing the following code?

public static void main(String args[])
{
        char digit = 'a';
        for (int i = 0; i < 10; i++)
        {
              switch (digit)
              {
                    case 'x' :
                    {
                        int j = 0;
                System.out.println(j);        
                    }
                    default :
                    {
                        int j = 100;
                System.out.println(j);        
                    }
              }
       }
       
    int i = j;
       System.out.println(i);    
}
---------------------------------------------------------------------------------
A. 100 will be printed 11 times. 
B. 100 will be printed 10 times and then there will be a runtime exception. 
C. The code will not compile because the variable i cannot be declared twice within the main() method. 
D. The code will not compile because the variable j cannot be declared twice within the switch statement.
E. None of these.

                       <PREVIOUS || Main Page ||  NEXT >
    Give your answers in the comment below

QUESTION 7

Considering the following code, which variables may be referenced correctly at line 12?

1.    public class Outer
2.    {
3.        public int a = 1;
4.        private int b = 2;
5.        public void method(final int c)
6.        {
7.            int d = 3;
8.            class Inner
9.            {
10.                private void iMethod(int e)
11.                 {
12. 
13.                }
14.            }
15.        }
16.    }
-------------------------------------------------------------------------------
A. a
B. b
C. c
D. d
E. e
                       <PREVIOUS || Main Page ||  NEXT >
    Give your answers in the comment below

QUESTION 6

In the following pieces of code, A and D will compile without any error. True/False?

A: StringBuffer sb1 = "abcd"; 
B: Boolean b = new Boolean("abcd"); 
C: byte b = 255; 
D: int x = 0x1234; 
E: float fl = 1.2;
-------------------------------------------------------------------------------------------
A. True
B. False
                     <PREVIOUS || Main Page ||  NEXT >
    Give your answers in the comment below

QUESTION 5

What results from attempting to compile and run the following code?

public class Ternary
{
    public static void main(String args[])
    {
        int a = 5;
        System.out.println("Value is - " + ((a < 5) ? 9.9 : 9));
    }
}
-----------------------------------------------------------------------------------
A.  prints: Value is - 9
B.  prints: Value is - 5
C.  Compilation error
D.  None of these
                        <PREVIOUS || Main Page || NEXT >
    Give your answers in the comment below

QUESTION 4

What will happen when you attempt to compile and run the following code?

interface MyInterface
{
}
public class MyInstanceTest implements MyInterface
{
    static String s;
    public static void main(String args[])
    {
        MyInstanceTest t = new MyInstanceTest();
        if(t instanceof MyInterface)
        {
            System.out.println("I am true interface");
        }
        else 
        {
            System.out.println("I am false interface");
        }
        if(s instanceof String)
        {
            System.out.println("I am true String");
        }
        else 
        {
            System.out.println("I am false String");
        }
    }    
}
-----------------------------------------------------------------------------------------
A.  Compile-time error
B.  Runtime error
C.  Prints : "I am true interface" followed by " I am true String"
D.  Prints : "I am false interface" followed by " I am false String"
E.  Prints : "I am true interface" followed by " I am false String" 
F.  Prints : "I am false interface" followed by " I am true String"
                   
                   <PREVIOUS || Main Page || NEXT >
    Give your answers in the comment below

QUESTION 3


What will happen when you attempt to compile and run the following code?

class MyParent 
{
    int x, y;
    MyParent(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
    public int addMe(int x, int y)
    {
        return this.x + x + y + this.y;
    }
    public int addMe(MyParent myPar)
    {
        return addMe(myPar.x, myPar.y);
    }
}
class MyChild extends MyParent
{
    int z;
    MyChild (int x, int y, int z)
    {
        super(x,y);
        this.z = z;
    }
    public int addMe(int x, int y, int z)
    {
        return this.x + x + this.y + y + this.z + z;
    }
    public int addMe(MyChild myChi)
    {
        return addMe(myChi.x, myChi.y, myChi.z);
    }
    public int addMe(int x, int y)
    {
        return this.x + x + this.y + y;
    }
}
public class MySomeOne
{
    public static void main(String args[])
    {
        MyChild myChi = new MyChild(10, 20, 30);
        MyParent myPar = new MyParent(10, 20);
        int x = myChi.addMe(10, 20, 30);
        int y = myChi.addMe(myChi);
        int z = myPar.addMe(myPar);
        System.out.println(x + y + z);
    }
}
-----------------------------------------------------------------------------------------
  A.  300
  B.  240
  C.  120
  D.  180
  E.  Compilation error
  F.  None of the above
                              <PREVIOUS || Main Page ||  NEXT >
    Give your answers in the comment below

QUESTION 2

What will happen when you attempt to compile and run the following code?

public class Static
{
    static
    {
        int x = 5;
    }
    static int x,y;
    public static void main(String args[])
    {
                x--;
        myMethod();
                System.out.println(x + y + ++x);
    }
    public static void myMethod()
    {
         y = x++ + ++x;
    }
}
--------------------------------------------------------------------------------------------
    A.  Compile-time error
    B.  prints : 1
    C.  prints : 2
    D.  prints : 3
    E.  prints : 7
    F.  prints : 8

                            <  PREVIOUS || Main Page || NEXT  >
Give your answer in the comment below

QUESTION 1

Q) What will happen when you attempt to compile and run the following code?

int Output = 10;
boolean b1 = false;
if((b1 == true) && ((Output += 10) == 20))
{
       System.out.println("We are equal " + Output);
}
else
{
        System.out.println("Not equal! " + Output);
}
---------------------------------------------------------
A)  Compilation error, attempting to perform binary comparison on logical data type.                                   B)  Compilation and output of "We are equal 10".          C)  Compilation and output of "Not equal! 20".            D)  Compilation and output of "Not equal! 10".
                    Main Page ||NEXT >
Give your answers in the comment below

Saturday 25 April 2015

" Civilization conquest " game in java code

This is a fabulous game code in java named as civilization conquest .You will definatly going to like this game as soon as you will play it.Actually this game is to find your friend address using map who is under arrest of thieves and in the way you have to face different problems

OUTPUT :

Click here to download source code
Note :
  1. After Click on "Click Here" please wait for 5 sec and than click on skip ad button to download the code
  2. If you like the program Please do comment below so i will post more program like that 

"Please do share this link with you friends and not forget to comment below if you like the game"

Wednesday 22 April 2015

Dice Game in java


This is a dice game in java code . Fantastic game roll the dice and start earn money with your competitor , The one with greater number on dice will get more money as compare to other so start game with your friend and see who will make more money

output:

        


Click Here to download the source code
Note :
  1. After Click on "Click Here" please wait for 5 sec and than click on skip ad button to download the code
  2. If you like the program Please do comment below so i will post more program like that 

Tuesday 21 April 2015

Hardware Management System


This is a very heavy project that can be useful for the people looking for latest java projects .

This project implement the GUI in most appropriate way and also support the database .

Actually this program store the data of a Hardware shop that have different item in it like Electrical,civil machinery and any item that it have.

This program will add new item to the file and delete the sold item from the file and also tells about the total stock present and total item sold and unsold

Note :
1) Run this program from the class "Menu.java"



OUTPUT:

               





 -- > " download " source code for this project
                            

                                  DOWNLOAD


Note :
  1. After Click on "Click Here" please wait for 5 sec and than click on skip ad button to download the code
  2. If you like the program Please do comment below so i will post more program like that 

Sunday 19 April 2015

Hospital Management System

Fabulous java program for "Hospital management System"

The following source code and examples are used for Hospital Management System. Some basic functions, such as Nurse/Wardboy Entry, Patient and Doctors Entry are included.
The source code and files included in this project are listed in the project files section, please make sure whether the listed source code meet your needs there.
This program will manipulate all the works occured within an hospital like Doctor, patient, room, ward , nurses records . entry/leaving of each person whether doctor, patient or any one else .
This program will definately helpful for final year student
Please use login user name of your local host and password is "YES"
Some of the picture of output is given below:


Click Here to download Hospital Management System


Note :
  1. After Click on "Click Here" please wait for 5 sec and than click on skip ad button to download the code
  2. If you like the program Please do comment below so i will post more program like that 

Friday 17 April 2015

Library Management System


This is a Remarkable  java project for final year Students

Modules Library management system project in java:

User Module:

In this module student will check availability of the manage book in library system. student just want to search book from Library system name in the search box.

Book Return :

Here student can return their books to the library. you have just click on return and type information of book and student id to submit result.

Administrator Module:

This is the most module within Library management system project in java. The administrator will browse and write info regarding any member. The administrator may update, produce and delete the record of membership as per demand and implementation plans.

Register student:

permit the administrator to register new student and update the coed records. to make available facility of library system.

Book details:

permit administrator to entered book details like book id, book name, book author, edition and more. Just download Library management system project in java.

Book issue:

Here administrator problems the books having code from library.

output


To download source click below :
                   
                          Download source code


Note :
  1. After Click on "Click Here" please wait for 5 sec and than click on skip ad button to download the code
  2. If you like the program Please do comment below so i will post more program like that 

Wednesday 15 April 2015

Loops

What is a Loop :

A loop means to cause a section of  program to be repeated a certain number of times.The repetition continues while a condition is true. When a condition becomes false , the loop ends and control passes to the statements following the loop.

Three types of loops:-
     1) For loop
     2) While loop
     3) Do-while loop

let us talk about each loop one by one


1) For loop:

    for( initialization ;  condition ; increment/decrement)
          for                              --> is a keyword
          initialization              --> starting value
          condition                   --> how much time loop execute
increment/decrement --> increase in value





'for' is a also known as counter controlled loop (we know how many time the loop execute before program run)

Example :

        #include<stdio.h>
int main(){
int a;
for( a=0 ;  a<=9 ; a++)
printf(“%d\n”,a);
return 0;
 }


 In this program the
    initial value --> 0
    Condition  a<=9 --> tells that loop will run 10 time   
    Increment/decrement--> a++ =show that there is increment of 1

Actually what happen :
At first run :
Program see the initial value of 'a'(i.e: 0) and check the condition (0<=9) whish is true so execute the statement and then make increment of '1'.

Second run --> last run :
Then again check the condition, know a=1(due to increment in ‘a’) as condition is true(1<=9) so again the loops body execute and increment of 1.and it will going on until the condition is true and when condition fail (i.e: 10<=9) then the loop terminate.




2) While loop :


Known as sentinel controlled loop(we don’t know how many times a loop execute),But it can also be counter controlled we see later how.

Sentinel value is the value, which is used to terminate the loop.

while(condition)
{
statement-1;
statement-2;

}

The “while loop” is like a simplified version of the “for loop”. It contains a test expression(condition) but no initialization or increment expression

Example:
             #include<stdio.h>
              int  main(){
int n = 0;
printf("enter no for square or enter 0 to exit = ");
scanf("%d",&n);
              while(n!=0){
printf("Square of %d = %d\n",n,n*n);
printf("enter no for square or enter 0 to exit = ");
scanf("%d",&n);
}
              return 0;
              }

In this program the ‘0’ is called the sentinel value,so if user enter value other than ‘0’ the condition become true and it will print square of number user enter,and program will continue untill user enter '0' but  when user enter the ‘0’condition become false and the loop terminte.

Actually what happen :

At start the compiler check condition  if condition is false, it will not enter into the loop and start executing the statement next to loop body and if condition is true the compiler enter into the body of loop and execute all the statement within the loop ,after execution of all the statement within the loop it will again check the condition if the condition is true it will again enter into the loop and Start executing statement and if condition is false, it will not enter into the loop and start executing the statement next to loop body.  

Tips :
1) If user enter ‘0’ at start, loop will not execute(i.e;it will not enter into the loop). and program terminate or move to statement next to the loop. 

 2) The statement you write above the condition should also be place at end of loop body as above;


3)      Do-while loop :-

Same as while loop some differences are there.The “do/while” structure tests the loop continuation condition after the loop body is performed. Therefore the loop body will be executed at least once.

        do{
Statement;
}while (condition);

 Example :
         #include<stdio.h>
         int main(){
    int n;
do{
    printf("Please enter number”);                    
                    scanf("%d",&n);
              printf("Number= %d \n" , n);
            printf("Square %d \n", n*n);
}while (n<=10);
return 0;
}


Monday 13 April 2015

"Reverse The Word" in java code

NOTE :
1) This program will reverse any word that you enter.
2) Save This program name as "StackChar.java" and run 


import javax.swing.JOptionPane;

public class StackChar {
 
    String st ;
    char[]  chr ;
    int top ;
 
    public StackChar(String stg){
        st = stg ;
        chr = new char[stg.length()];
        top=-1;
    }
 
    public void pushChar(char num){
        if(top==st.length()-1){
            System.out.println("NO FREE SPACE ");
            return;
        }
        else
            chr[++top]=num ;
    }
 
    public char popChar(){
        char item ;
        if(top==-1){
            System.out.println("ALREADY Empty");
            return 0 ;
        }
        else
            item = chr[top--];
            return item;
    }
 
     public char peep(){
        if(top==-1){
            System.out.println("IS EMPTY");
            return 0 ;
        }
        return chr[top];
         
    }
    public void reverseString(){
String n = "";
        for(int i=0;i<st.length();i++){
            char ch = st.charAt(i);
            pushChar(ch);
        }
        for(int i=0;i<st.length();i++){
            n+=popChar();
        }

JOptionPane.showMessageDialog(null, n , " Name reversed", 1);
    }


public static void main(String[] args){

String firstName = JOptionPane.showInputDialog("INVERT THE NAME ",""
                + "Enter your name " );
StackChar stk = new StackChar(firstName);
stk.reverseString();
}
}

OUTPUT :



Sunday 12 April 2015

How to install JDK(Java development kit)?

These instructions are to help you download and install Java on your personal computer. You must install Java before installing Eclipse, and you will need both.


Downloading and Installing Java On Windows:


Prevent Errors like --> 'javac' is not recognized as an internal or external command

1. Click on "download" to download the latest Version of Jave SDK or any Jace SDK as per your requirement and install on your system.

2. Accept all of the defaults and suggestions, but make sure that the location where Java will be installed is at the top level of your C: drive. Click on "Finish." You should have a directory (folder) named C:\j2sdk1.5.0_04, with subfolders C:\j2sdk1.5.0_04\bin and C:\j2sdk1.5.0_04\lib

4. Modify your system variable called "PATH" (so that programs can find where Java is located).
To do this for Windows 2000 or XP, either right-click on the My Computer icon or select "System" on the control panel. When the window titled "System Properties" appears, choose the tab at the top named "Advanced." Then, click on "Environment Variables." In the bottom window that shows system variables, select "Path" and then click on "Edit..." Add C:\j2sdk1.5.0_04\bin as the first item in the list. Note that all items are separated by a semicolon, with no spaces around the semicolon. You should end up with a path variable that looks something like
C:\j2sdk1.5.0_04\bin;C:\WINNT\system32;C:\WINNT;C:\WINNT\system32\Wbem

For Windows 98 or ME, open the file AUTOEXEC.BAT in Notepad. You should find a line in this file that begins
SET PATH=...
Modify this line to add C:\j2sdk1.5.0_04\bin; immediately after the equals sign.

5. Modify or create a system variable called "CLASSPATH," as follows. In the lower "System Variables" pane choose "New..." and type in Variable Name "CLASSPATH" and value (note that it begins with dot semicolon)
.;C:\j2sdk1.5.0_04\lib

6. To test Java to see if everything is installed properly, open a command window (a DOS window) and type the command "javac" The result should be information about the Usage of javac and its options. If you get a result that "'javac' is not recognized as an internal or external command, operable program or batch file" then there is a problem and Java will not work correctly.

Wednesday 8 April 2015

"Mind Reader" in java code



Note :
1) Compile the program named as MindReader.java
2) In This program you have to think some number and after performing some calculation on that number , this program will tell the final answer .

import java.util.Random;
import javax.swing.JOptionPane;


public class MindReader {


     public static void main(String[] args) {
     
        Random ran = new Random();
        JOptionPane.showMessageDialog(null, "Think of some number in your mind(numeric),"
                + "\n and do following steps ", "Mind Reader" , 1);
     
        double d = ran.nextInt(199)+1;
        JOptionPane.showMessageDialog(null, "STEPS :     \n1)Double the number that you think "
                + "\n2) Add " + d + " to that number "
                + "\n3) Half that number "
                + "\n4) subtract the number you think from the present value", "Mind Reader" , 1);
        float f = (float)(d/2);
        JOptionPane.showMessageDialog(null, "The Final answer will be " + f, "Mind Reader" , 1);
    }
 
}

Sample output :


Monday 6 April 2015

Love calulator in java code

Note :-
- This program will calculate the percentage of love between two person.
- Only write first name (not full name).
- Compile this program named as "Calculator.java" and than run.
- Sample output is given below

import java.util.ArrayList;
import javax.swing.JOptionPane;

public class Calculator {
    static ArrayList<Integer> it = new ArrayList<Integer>();
    static String fin;
    static String s="";
    public static void main(String[] args) {
     
        String firstName = JOptionPane.showInputDialog("Enter your name " );
        String secondName = JOptionPane.showInputDialog("Enter your Partner name " );
        int num;
        Other o = new Other();
        String sen = firstName + "loves" + secondName;
        String sent = sen.toUpperCase();
     
        char[] ch = sent.toCharArray();
        for(int i=0 ; i<sent.length(); i++){
            num=1;
            char chr = sent.charAt(i);
            if(o.check(chr)){
            }
            else{
                o.put(chr);
             
                for(int j=i+1 ; j<sent.length();j++){
                    if(chr==ch[j])
                        num+=1;
                }
                it.add(num);
            }
        }
        cal();
        display();
        int n = Integer.parseInt(s);
        if(n>100)
            n=100;
        JOptionPane.showMessageDialog(null, firstName.toUpperCase() + " LOVES TO " + secondName.toUpperCase() + " "+ n + "%", "Love Percentage", 1);
    }
    public static void display(){
     
            for(int i :it){
                s += i;
            }
         
         
    }
 
    public static void cal(){
        int j;
        j=it.size();
       for(int k=0;it.size()>2;k++){
        for(int i=0;i<(j-1);i++){
            int sum = it.get(i) + it.get(j-1);
            it.remove(j-1);
            it.remove(i);
            it.add(i, sum);
         
           j=it.size();
        }
     
       }
     
    }
 
}

 class Other {
 
    ArrayList<Character> ch = new ArrayList<Character>();
 
    public boolean check(char c){
        for(int i=0;i<ch.size();i++){
            if(c==ch.get(i))
                return true;
        }
        return false;
    }
 
    public void put(char c){
        ch.add(c);
     
    }
 
}

Sample output





Sunday 5 April 2015

Method Overloading in java

When a class has more than one method with same name, then we call that method is overloaded. The overloaded methods will have different number of arguments or different types of arguments, but name of the methods remains same.
Compiler checks method signature for duplicate methods or for method overloading. method signature consist of three things,
 1) Method Name   
 2) Number Of Arguments  
 3) Types of arguments.
If these three things are same for any two methods in a class, then compiler gives duplicate method error.
Compiler first checks method name. If it is same, then it checks number of arguments. If methods differs in number of arguments, then it does not check types of argument. It treats as methods are overloaded. If number of arguments are same then compiler checks types of arguments. If types of arguments are also same, then compiler will give duplicate method error. If types of arguments are not same, then compiler will treat them as methods are overloaded.
For method overloading to be successful, method name must be same and methods must have different number of arguments or different types of arguments. If method names are different, then those methods will be treated as two different methods.
Go through this example,
public class MethodOverloading
{
void methodOverloaded()
{
//No argument method
}
void methodOverloaded(int i)
{
//One argument is passed
}
void methodOverloaded(double d)
{
//One argument is passed but type of argument is different
}
void methodOverloaded(int i, double d)
{
//Two argument method
//Method signature of this method is methodOverloaded(int, double)
}
void methodOverloaded(double d, int i)
{
//It is also two argument method but type of arguments changes
//Method signature of this method is methodOverloaded(double, int)
}
void methodOverloaded(double d1, int i1)
{
                //It has same method signature methodOverloaded(double, int) as of above method
//So, it is a Duplicate method, You will get compile time error here
}
    void differentMethod()
{
//Different method
}
}
Overloaded methods may have same return types or different return types. It does not effect method overloading.
public class MethodOverloading
{
void methodOverloaded()
{
//No argument method, return type is void
}
int methodOverloaded(int i)
{
        //Returns int type
return i;
}
int methodOverloaded(double d)
{
        //Same return type as of above method
return 0;
}
void methodOverloaded(double d)
{
//Duplicate method because it has same method signature as of above method
}
}
Important Note :
 If two methods have same signature and different return types, then those methods will not be treated as two different methods or methods overloaded. For duplication, compiler checks only method signature not return types. If method signature is same, straight away it gives duplicate method error.
Overloaded methods may have same access modifiers or different access modifiers. It also does not effect method overloading.
public class MethodOverloading
{
private void methodOverloaded()
{
//No argument, private method
}
private int methodOverloaded(int i)
{
//One argument private method
return i;
}
protected int methodOverloaded(double d)
{
//Protected Method
return 0;
}
public void methodOverloaded(int i, double d)
{
//Public Method
}
}
Overloaded methods may be static or non-static. This also does not effect method overloading.
public class MethodOverloading
{
private static void methodOverloaded()
{
//No argument, private static method
}
private int methodOverloaded(int i)
{
//One argument private non-static method
return i;
}
static int methodOverloaded(double d)
{
//static Method
return 0;
}
public void methodOverloaded(int i, double d)
{
//Public non-static Method
}
}
From the above examples, it is clear that compiler will check only method signature for method overloading or for duplicate methods. It does not check return types, access modifiers and static or non-static.