Sample Questions Best site for GRE, LSAT, SAT, GMAT, TOEFL, CCNA, CCSA and interview sample questions  

Technical Sample Questions : C |  C++ |  Oracle |  Java | Unix |  Operating Systems |  Data Structure

Java Sample Questions ›› Java questions

Technical General Java Sample Questions

  1. What is the purpose of the following subroutine? What is the meaning of the value that it returns, in terms of the value of its parameter?

    static String concat( String[] str ) {
    if (str == null)
    return "";
    String ans = "";
    for (int i = 0; i < str.length; i++) {
    ans = ans + str[i];
    return ans;
    }

    Answer: The purpose of the subroutine is to chain all the strings in an array of strings into one long string.If the array parameter is null, then there are no strings, and the empty string is returned.Otherwise, the value returned is the string made up of all the strings from the array.For example, if stringList is an array declared as String[] stringList = { "Put 'em ", "all", " together" };
    then the value of concat(stringList) is "Put 'em all together".

  2. Show the exact output produced by the following code segment.

    char[][] pic = new char[6][6];
    for (int i = 0; i < 6; i++)
    for (int j = 0; j < 6; j++) {
    if ( i == j || i == 0 || i == 5 )
    pic[i][j] = '*';
    else
    pic[i][j] = '.';
    }
    for (int i = 0; i < 6; i++)
    {
    for (int j = 0; j < 6; j++)
    System.out.print(pic[i][j]);
    System.out.println();
    }

    Answer: The output consists of six lines, with each line containing six characters.In the first line, i is 0, so all the characters are *'s.In the last line, i is 5, so all the characters are *'s.In each of the four lines in the middle, one of the characters is a * and the rest are periods.The output is

    ******
    .*....
    ..*...
    ...*..
    ....*.
    ******

    It might help to look at the array items that are printed on each line.Note that pic[row][col] is '*' if row is 0 or if row is 5 or if row and col are equal.

    pic[0][0] pic[0][1] pic[0][2] pic[0][3] pic[0][4] pic[0][5]
    pic[1][0] pic[1][1] pic[1][2] pic[1][3] pic[1][4] pic[1][5]
    pic[2][0] pic[2][1] pic[2][2] pic[2][3] pic[2][4] pic[2][5]
    pic[3][0] pic[3][1] pic[3][2] pic[3][3] pic[3][4] pic[3][5]
    pic[4][0] pic[4][1] pic[4][2] pic[4][3] pic[4][4] pic[4][5]
    pic[5][0] pic[5][1] pic[5][2] pic[5][3] pic[5][4] pic[5][5]

  3. Write a complete subroutine that finds the largest value in an array of ints.The subroutine should have one parameter, which is an array of type int[].The largest number in the array should be returned as the value of the subroutine.

    Answer: public static int getMax(int[] list) {
    int max = list[0];
    for (int i = 1; i < list.length; i++) {
    if (list[i] > max)
    max = list[i];
    }
    return max;
    }

  4. What is a precondition? Give an example.

    Answer: A precondition is a condition that has to hold at given point in the execution of a program, if the execution of the program is to continue correctly.For example, the statement "x = A[i];" has two preconditions: that A is not null and that 0 <= i < A.length.If either of these preconditions is violated, then the execution of the statement will generate an error.

    Also, a precondition of a subroutine is a condition that has to be true when the subroutine is called in order for the subroutine to work correctly.

  5. Is "abc" a primitive value?

    Answer: The String literal "abc" is not a primitive value. It is a String object.

  6. What restrictions are placed on the values of each case of a switch statement?

    Answer: During compilation, the values of each case of a switch statement must evaluate to a value that can be promoted to an int value.

  7. What modifiers may be used with an interface declaration?

    Answer: An interface may be declared as public or abstract.

  8. Is a class a subclass of itself?

    Answer: A class is a subclass of itself.

  9. What is the difference between a while statement and a do statement?

    Answer: A while statement checks at the beginning of a loop to see whether the next loop iteration should occur. A do statement checks at the end of a loop to see whether the next iteration of a loop should occur. The do statement will always execute the body of a loop at least once.

  10. What modifiers can be used with a local inner class?

    Answer: A local inner class may be final or abstract.

« Previous || Next »

Java Sample Question Number: 1-10| 11-20| 21-30| 31-40| 41-50| 51-60| 61-70| 71-80| 81-90| 91-100| 101-110| 111-120| 121-130| 131-140| 141-150| 151-160| 161-170| 171-180| 181-190| 191-200| 201-210| 211-220| 221-230| 231-240| 241-250| 251-260| 261-270| 271-280| 281-290| 291-300| 301-310| 311-320| 321-330
Sample Test Questions
GRE Sample Questions
CAT Sample Questions
GMAT Sample Questions
TOEFL Sample Questions
ACT Sample Questions
SAT Sample Questions
LSAT Sample Questions
PSAT Sample Questions
MCAT Sample Questions
PMP Sample Questions
GED Sample Questions
ECDL Sample Questions
DMV Sample Questions
CCNA Sample Questions
MCSE Sample Questions
Network+ Sample Questions
A+ Sample Questions
Technical Sample Questions
WASL Sample Questions
CISA Sample Questions

Other Sample Questions
Sample Interview Questions
Sample Teacher Interview Questions
Sample Citizenship Questions
Accuplacer Sample Questions
Science Bowl sample Questions
Driving Test Sample Questions
Sample Survey Questions Sample Essay Questions
Sample Behavioral Interview Questions

Copyright © 2004-2013, Best BSQ. All Rights Reserved.