ALLInterview.com :: Home Page KalAajKal.com
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
Do you have a collection of Interview Questions and interested to share with us!!
Please send that collection to along with your userid / name. ThanQ
Google
 
Categories  >>  Software  >>  Core Java  >>  Java J2EE  >>  Java Related
 
 


 

 
 Core Java interview questions  Core Java Interview Questions
 Advanced Java interview questions  Advanced Java Interview Questions
 Swing interview questions  Swing Interview Questions
 EJB interview questions  EJB Interview Questions
 Servlets interview questions  Servlets Interview Questions
 Struts interview questions  Struts Interview Questions
 JDBC interview questions  JDBC Interview Questions
 JMS interview questions  JMS Interview Questions
 SunOne interview questions  SunOne Interview Questions
 J2EE interview questions  J2EE Interview Questions
 Weblogic interview questions  Weblogic Interview Questions
 Websphere interview questions  Websphere Interview Questions
 Java Networking interview questions  Java Networking Interview Questions
 Java J2EE AllOther interview questions  Java J2EE AllOther Interview Questions
Question
write java code to print second max number in the array
 Question Submitted By :: Guest
I also faced this Question!!     Rank Answer Posted By  
 
  Re: write java code to print second max number in the array
Answer
# 1
public static void main(String[] args){
    int maxNumber = 0;
    int secondMaxNumber = 0;
    if(args.length == 0){
      System.err.println("Number array is empty");
      return;
    }
    for(int i=0; i < args.length; i++){
      int currNumber = Integer.parseInt(args[i]);
      if(maxNumber < currNumber){
        secondMaxNumber = maxNumber;
        maxNumber = currNumber;
      }else if(secondMaxNumber < currNumber){
        secondMaxNumber = currNumber;
      }
    }
    System.err.println("Max. number is "+maxNumber);
    System.err.println("Second Max. is "+secondMaxNumber);
  }
}
 
Is This Answer Correct ?    9 Yes 5 No
Rajaram
 
  Re: write java code to print second max number in the array
Answer
# 2
public class SecondMaximumNumber{
  public static void main(String[] args){
    int maxNumber = 0;
    int secondMaxNumber = 0;
    if(args.length == 0){
      System.err.println("Number array is empty");
      return;
    }
    for(int i=0; i < args.length; i++){
      int currNumber = Integer.parseInt(args[i]);
      if(maxNumber < currNumber){
        secondMaxNumber = maxNumber;
        maxNumber = currNumber;
      }else if(secondMaxNumber < currNumber){
        secondMaxNumber = currNumber;
      }
    }
    System.err.println("Max. number is "+maxNumber);
    System.err.println("Second Max. number is
"+secondMaxNumber);
  }
}
 
Is This Answer Correct ?    4 Yes 3 No
Raja Ram
 
 
 
  Re: write java code to print second max number in the array
Answer
# 3
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;


public class SecondMax {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		int[] numbers = {9,4,8,0,0,5,9,1,4,2};
		Set arrSet = new HashSet();
		for (int i=0;i<numbers.length;i++) {
			arrSet.add(numbers[i]);
		}
		ArrayList s = new ArrayList(arrSet);
		Collections.sort(s);

		System.out.println("Second element : " + 
s.get(s.size()-2));
	}
}
 
Is This Answer Correct ?    6 Yes 1 No
Himesh Mistry
 
  Re: write java code to print second max number in the array
Answer
# 4
Himesh Mistry -best solution
 
Is This Answer Correct ?    4 Yes 1 No
Geetha
 
  Re: write java code to print second max number in the array
Answer
# 5
Ranjaram is correct.I modified and testes it.
public class test123 {
	public static void main(String[] args){
	    int maxNumber = 0;
	    int secondMaxNumber = 0;
	  
	    int[] anArray;           
	    anArray =new int [10];
	    anArray[0] = 100; 
        anArray[1] = 200; 
        anArray[2] = 300; 
        anArray[3] = 400;
        anArray[4] = 500;

	    if(anArray.length == 0){
	      System.err.println("Number array is empty");
	      return;
	    }
	   for(int i=0; i < anArray.length; i++){
	     int currNumber = anArray[i];
	      if(maxNumber < currNumber){
	        secondMaxNumber = maxNumber;
	        maxNumber = currNumber;
	      }else if(secondMaxNumber < currNumber){
	        secondMaxNumber = currNumber;
	      }
	    }
	    System.err.println("Max. number is "+maxNumber);
	    System.err.println("Second Max. 
is "+secondMaxNumber);
	  }
	}
 
Is This Answer Correct ?    2 Yes 2 No
Sujeev
 
  Re: write java code to print second max number in the array
Answer
# 6
I think, the except for Himesh's program, all other ones 
doesnt handle duplicate values ...
I mean if the numbers are :- 9 9 8 8 7 7 7 1 3 2
the answer should be 8 ideally.
But I doubt whether thats really happening in any other 
programs, except for Himesh's.
Do let me know, if I am wrong. Thanks.
 
Is This Answer Correct ?    4 Yes 0 No
Sunny
 
  Re: write java code to print second max number in the array
Answer
# 7
hey using collections solving this type of problem becomes
easy but whenever u go for interview most of the time they
ask you to write program without using collection API thts
why i think rajaram's code is correct
 
Is This Answer Correct ?    1 Yes 0 No
Hitman
 
  Re: write java code to print second max number in the array
Answer
# 8
If we give negative values, what would be the Rajaraman 
code begaves????
 
Is This Answer Correct ?    0 Yes 1 No
Rajesh S
 
  Re: write java code to print second max number in the array
Answer
# 9
This is I tried.
public class SecondLargestNumber {
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		int[] num = new int[]{-101,-105,-2,-7,-22,-
104,-8,-10,-100,-102,-102};
		int big=-1;
		int secbig=-1;
		if(num.length == 1) {
			big = num[0];
			secbig = num[0];
		} else {
			if(num[0] > num[1]) {
				big = num[0];
				secbig = num[1];
			} else {
				big = num[1];
				secbig = num[0];
			}
		} 
		if(num.length > 2){
			for(int i=2;i<num.length;++i){
				if(num[i] > secbig && num
[i] <big) {
					secbig = num[i];
				}
				if(num[i]>=big){
					secbig = big;
					big = num[i];
				}
			}
		}
		System.out.println(big);
		System.out.println(secbig);
	}
}

Please check is it working and let me know. For removing 
duplicates, we will have one more method to remove the 
duplicate elements.
 
Is This Answer Correct ?    2 Yes 0 No
Rajesh S
 

 
 
 
Other Core Java Interview Questions
 
  Question Asked @ Answers
 
what is struts-config-xml?and its use?  3
What is the basic functionality of DataOutput interface in java? TCS2
Explain final, finalize() and finally?  6
How the elements are organized in CardLayout?  5
What classes of exceptions, caught by a catch clause?  1
How are commas used in the initialization and iteration parts of a for statement?  2
when we have to use final class in java?  2
Difference between Choice and a List?  1
How will you override default serialization mechanism in java  2
What is the diff. b/w Interfaces & Abstract class? Ericsson4
In Serialization, whether you will use Static variables? HCL2
what is java bean?where can we use it? TCS3
what are the methods of an object class?  3
Name the method of a Container that can be used to cause a container to be laid out and redisplayed?  1
why abstract class will have a constructor?  3
how can u handle run time exception in java? explain with brief explanation with examples? CTS2
can you program for reverse string? IBM5
How two different class threads communicate with each other?. send example code.  4
When does the compiler supply a default constructor for a class? TCS8
What are different type of access modifiers?  3
 
For more Core Java Interview Questions Click Here 
 
 
 
 
 
   
Copyright Policy  |  Terms of Service  |  Help  |  Site Map 1  |  Articles  |  Site Map  |   Site Map  |  Contact Us interview questions urls   External Links 
   
Copyright © 2007  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  Forum9.com   ::  KalAajKal.com