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. A subroutine is said to have a contract.What is meant by the contract of a subroutine? When you want to use a subroutine, why is it important to understand its contract? The contract has both "syntactic" and "semantic" aspects.What is the syntactic aspect? What is the semantic aspect?

    Answer: The contract of a subroutine says what must be done to call the subroutine correctly and what it will do when it is called.It is, in short, everything a programmer needs to know about the subroutine in order to use it correctly.(It does not include the "insides," or implementation, of the subroutine.)

    The syntactic component of a subroutine's contract includes the name of the subroutine, the number of parameters, and the type of each parameter.This is the information needed to write a subroutine call statement that can be successfully compiled.The semantic component of the contract specifies the meaning of the subroutine, that is, the task that the subroutine performs.It might also specify limitations on what parameter values the subroutine can process correctly.The semantic component is not part of the program.It is generally expressed in comments.

  2. Briefly explain how subroutines can be a useful tool in the top-down design of programs.

    Answer: Top-down refers to starting from the overall problem to be solved, and breaking it up into smaller problems that can be solved separately.When designing a program to solve the problem, you can simply make up a subroutine to solve each of the smaller problems.Then you can separately design and test each subroutine.

  3. Discuss the concept of parameters.What are parameters for? What is the difference between formal parameters and actual parameters?

    Answer: Parameters are used for communication between a subroutine and the part of the program that calls the subroutine.If a subroutine is thought of as a black box, then parameters are part of the interface to that black box.Formal parameters are found in the subroutine definition.Actual parameters are found in subroutine call statements.When the subroutine is called, the values of the actual parameters are assigned to the formal parameters before the body of the subroutine is executed.

  4. Give two different reasons for using named constants

    Answer: A constant has a meaningful name, which makes the program easier to read.It's easier to understand what a name like INTEREST_RATE is for than it is to figure out how a literal number like 0.07 is being used.

    A second reason for using named constants is that it's easy to modify the value of the constant if that becomes necessary.If a literal value is used throughout the program, the programmer has to track down each occurrence of the value and change it.When a constant is used correctly, it is only necessary to change the value assigned to the constant at one point in the program.

    A third reason is that using the final modifier protects the value of a variable from being changed.This is especially important for member variables that are accessible from outside the class where they are declared.

  5. What is an API? Give an example.

    Answer: An API is an Applications Programming Interface.It is the interface to a "toolbox" of subroutines that someone has written.It tells you what routines are available, how to call them, and what they do, but it does not tell you how the subroutines are implemented.An example is the standard Java API which describes the interfaces of all the subroutines in all the classes that are available in such packages as java.lang and java.awt.

  6. Write a subroutine named "stars" that will output a line of stars to standard output.(A star is the character "*".) The number of stars should be given as a parameter to the subroutine.Use a for loop.For example, the command "stars(20)" would output

    ********************

    Answer: The subroutine could be written as follows.

    static void stars(int numberOfStars) {
    for (int i = 0; i < numberOfStars; i++) {
    System.out.print('*');
    }
    System.out.println();
    }

  7. Write a main() routine that uses the subroutine that you wrote for Question 7 to output 10 lines of stars with 1 star in the first line, 2 stars in the second line, and so on, as shown below. *
    **
    ***
    ****
    *****
    ******
    *******
    ********
    *********
    **********

    Answer: The main() routine can use a for loop that calls the stars() subroutine ten times, once to produce each line of output.(An occasional beginner's mistake in this problem is to rewrite the body of the subroutine inside the main() routine, instead of just calling it by name.) Here is the main routine -- which would, of course, have to be put together with the subroutine in a class in order to be used.

    public static void main(String[] args) {
    int line;
    for ( line = 1;line <= 10;line++ ) {
    stars( line );
    }
    }

  8. Write a function named countChars that has a String and a char as parameters.The function should count the number of times the character occurs in the string, and it should return the result as the value of the function.

    Answer: The returned value will be of type int.The function simply uses a for loop to look at each character in the string.When the character in the string matches the parameter value, it is counted.

    static int countChars( String str, char searchChar ) {
    int i;
    char ch;
    int count;
    count = 0;
    for ( i = 0;i < str.length();i++ ) {
    ch = str.charAt(i);
    if ( ch == searchChar )
    count++;
    }
    return count;
    }

  9. Write a subroutine with three parameters of type int.The subroutine should determine which of its parameters is smallest.The value of the smallest parameter should be returned as the value of the subroutine.

    Answer: I'll call the subroutine smallest and the three parameters x, y, and z.The value returned by the subroutine has to be either x or y or z.The answer will be x if x is less than or equal to both y and z.The correct syntax for checking this is "if (x <= y && x <= z).Similarly for y.The only other remaining possibility is z, so there is no necessity for making any further test before returning z.

    static int smallest(int x, int y, int z) {
    if (x <= y && x <= z) {
    return x;
    }
    else if (y <= x && y <= z) {
    return y;
    }
    else
    return z;
    }

    Note: Since a return statement causes the computer to terminate the execution of a subroutine anyway, this could also be written as follows, without the elses:

    static int smallest(int x, int y, int z) {
    if (x <= y && x <= z) {
    return x;
    }
    if (y <= x && y <= z) {
    return y;
    }
    return z;
    }

  10. Object-oriented programming uses classes and objects.What are classes and what are objects? What is the relationship between classes and objects?

    Answer: When used in object-oriented programming, a class is a factory for creating objects.(We are talking here about the non-static part of the class.) An object is a collection of data and behaviors that represent some entity (real or abstract).A class defines the structure and behaviors of all entities of a given type.An object is one particular "instance" of that type of entity.For example, if Dog is a class, than Lassie would be an object of type Dog.

« 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.