0% found this document useful (0 votes)
68 views9 pages

Java Questions

Uploaded by

myhealth632
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
68 views9 pages

Java Questions

Uploaded by

myhealth632
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 9

Source of Internet

(http://docs.oracle.com/javase/tutorial/java/javaOO/index.html)

VARIABLES QUESTIONS

1. The term "instance variable" is another name for *Non Static


Variable*___.
2. The term "class variable" is another name for ___.
3. A local variable stores temporary state; it is declared inside a
*Method*.
4. A variable declared within the opening and closing parenthesis
of a method signature is called a *_Paramaters*___.
5. What are the eight primitive data types supported by the Java
programming language?
6. Character strings are represented by the class *String*___.
7. An *Array* is a container object that holds a fixed number of
values of a single type.

Exercises

1. Create a small program that defines some fields. Try creating


some illegal field names and see what kind of error the
compiler produces. Use the naming rules and conventions as a
guide.
2. In the program you created in Exercise 1, try leaving the fields
uninitialized and print out their values. Try the same with a
local variable and see what kind of compiler errors you can
produce. Becoming familiar with common compiler errors will
make it easier to recognize bugs in your code.

OPERATORS

Questions
1. Consider the following code snippet.

arrayOfInts[j] > arrayOfInts[j+1]

Which operators does the code contain?

2. Consider the following code snippet.

int i = 10;
int n = i++%5;

a. What are the values of i and n after the code is


executed?
b. What are the final values of i and n if instead of using
the postfix increment operator (i++), you use the prefix
version (++i))?
3. To invert the value of a boolean, which operator would you
use?
4. Which operator is used to compare two values, = or == ? ==
5. Explain the following code sample: result = someCondition ?
value1 : value2;(If-statement)

Exercises

1. Change the following program to use compound assignments:


2. class ArithmeticDemo {
3.
4. public static void main (String[] args){
5.
6. int result = 1 + 2; // result is now 3
7. System.out.println(result);
8.
9. result = result - 1; // result is now 2
10. System.out.println(result);
11.
12. result = result * 2; // result is now 4
13. System.out.println(result);
14.
15. result = result / 2; // result is now 2
16. System.out.println(result);
17.
18. result = result + 8; // result is now 10
19. result = result % 7; // result is now 3
20. System.out.println(result);
21. }
22. }
23.
24. In the following program, explain why the value "6" is
printed twice in a row:
25. class PrePostDemo {
26. public static void main(String[] args){
27. int i = 3;
28. i++;
29. System.out.println(i); // "4"
30. ++i;
31. System.out.println(i); // "5"
32. System.out.println(++i); // "6"
33. System.out.println(i++); // "6"
34. System.out.println(i); // "7"
35. }
36. }
EXPRESSIONS AND STATEMENT BLOCKS

Questions

1. Operators may be used in building ___, which compute values.


2. Expressions are the core components of ___.
3. Statements may be grouped into ___.
4. The following code snippet is an example of a ___ expression.
5. 1 * 2 * 3
6. Statements are roughly equivalent to sentences in natural
languages, but instead of ending with a period, a statement
ends with a ___.
7. A block is a group of zero or more statements between
balanced ___ and can be used anywhere a single statement is
allowed.

Exercises

Identify the following kinds of expression statements:

• aValue = 8933.234;
• aValue++;
• System.out.println("Hello World!");
• Bicycle myBike = new Bicycle();

CONTROL FLOW STATEMENTS

Questions

1. The most basic control flow statement supported by the Java


programming language is the ___ statement.
2. The ___ statement allows for any number of possible execution
paths.
3. The ___ statement is similar to the while statement, but
evaluates its expression at the ___ of the loop.
4. How do you write an infinite loop using the for statement?
5. How do you write an infinite loop using the while statement?

Exercises

1. Consider the following code snippet.


2. if (aNumber >= 0)
3. if (aNumber == 0)
4. System.out.println("first string");
5. else System.out.println("second string");
6. System.out.println("third string");
a. What output do you think the code will produce if
aNumber is 3?
b. Write a test program containing the previous code
snippet; make aNumber 3. What is the output of the
program? Is it what you predicted? Explain why the output
is what it is; in other words, what is the control flow for
the code snippet?
c. Using only spaces and line breaks, reformat the code
snippet to make the control flow easier to understand.
d. Use braces, { and }, to further clarify the code.

CLASSES QUESTIONS

Questions

1. Consider the following class:

public class IdentifyMyParts {


public static int x = 7;
2. public int y = 3;
}

a. What are the class variables?


b. What are the instance variables?
c. What is the output from the following code:

IdentifyMyParts a = new IdentifyMyParts();


IdentifyMyParts b = new IdentifyMyParts();
a.y = 5;
b.y = 6;
a.x = 1;
b.x = 2;
System.out.println("a.y = " + a.y);
System.out.println("b.y = " + b.y);
System.out.println("a.x = " + a.x);
System.out.println("b.x = " + b.x);
System.out.println("IdentifyMyParts.x = " + IdentifyMyParts.x);

3. Write a class whose instances represent a single playing card


from a deck of cards. Playing cards have two distinguishing
properties: rank and suit. Be sure to keep your solution as you
will be asked to rewrite it in Enum Types.

Hint:
You can use the assert statement to check your assignments.
You write:
assert (boolean expression to test);
If the boolean expression is false, you will get an error message.
For example,
assert toString(ACE) == "Ace";
should return true, so there will be no error message.
If you use the assert statement, you must run your program with
the ea flag:
java -ea YourProgram.class

· Write a class whose instances represent a full deck of cards. You


should also keep this solution.

· 3. Write a small program to test your deck and card classes. The
program can be as simple as creating a deck of cards and
displaying its cards.

INHERITANCES
Questions

1. Consider the following two classes:

public class ClassA {


public void methodOne(int i) {
}
public void methodTwo(int i) {
}
public static void methodThree(int i) {
}
public static void methodFour(int i) {
}
}

public class ClassB extends ClassA {


public static void methodOne(int i) {
}
public void methodTwo(int i) {
}
public void methodThree(int i) {
}
public static void methodFour(int i) {
}
}

a. Which method overrides a method in the superclass?


b. Which method hides a method in the superclass?
c. What do the other methods do?

2. Consider the Card, Deck, and DisplayDeck classes you wrote in


Questions and Exercises: Classes. What Object methods should each
of these classes override?

Exercises
1. Write the implementations for the methods that you answered in
question 2.

NUMBERS

Questions

1. Use the API documentation to find the answers to the


following questions:
a. What Integer method can you use to convert an int into
a string that expresses the number in hexadecimal? For
example, what method converts the integer 65 into the
string "41"?
b. What Integer method would you use to convert a string
expressed in base 5 into the equivalent int? For example,
how would you convert the string "230" into the integer
value 65? Show the code you would use to accomplish
this task.
c. What Double method can you use to detect whether a
floating-point number has the special value Not a Number
(NaN)?
2. What is the value of the following expression, and why?
3. Integer.valueOf(1).equals(Long.valueOf(1))

Exercises

1. Change MaxVariablesDemo to show minimum values instead of


maximum values. You can delete all code related to the
variables aChar and aBoolean. What is the output?
2. Create a program that reads an unspecified number of integer
arguments from the command line and adds them together.
For example, suppose that you enter the following:
3. java Adder 1 3 2 10
The program should display 16 and then exit. The program
should display an error message if the user enters only one
argument. You can base your program on ValueOfDemo.

4. Create a program that is similar to the previous one but has


the following differences:
o Instead of reading integer arguments, it reads floating-
point arguments.
o It displays the sum of the arguments, using exactly two
digits to the right of the decimal point.

For example, suppose that you enter the following:

java FPAdder 1 1e2 3.0 4.754

The program would display 108.75. Depending on your locale,


the decimal point might be a comma (,) instead of a period (.).

STRING

You might also like