C Programs Tutorials | IT Developer
IT Developer

Java Programs



Share with a Friend

Short Questions/Answers - Java Programs

Question 1

a) Name any four tokens of Java.
Identifiers, Keywords, Literals, Operators.
b) Give the difference between actual parameter and formal parameter.
The parameters passed during function call are actual parameters. The parameters used during function definition are formal parameters.
c) What is an identifier?
Identifiers are the variables that are the named storage locations to store values temporarily in a program.
d) Write an expression in Java for

Math.cos(x) + Math.pow(a * a + b * b)
e) What is the result produced by 2 – 10 * 3 + 100 / 11? Show the steps.
2 – 10 * 3 + 100 / 11
= 2 – 30 + 100 / 11
= 2 – 30 + 9
= -28 + 9
= -19.

Question 2

a) What is the difference between local variable and instance variable?
The variables declared inside a function are called local variables. The variables declared inside a class so that each object gets its own copy of data members are called instance variables.
b) int x = 20, y = 10, z;
z = ++x * (y–) – y?
Show the steps.
z = 21 * 10 – 9
z = 210 – 9
z = 201.
c) What is the purpose of default in a switch?
The default case provides a case that gets executed when no other case is matched.
d) Give the difference between linear search and binary search.
Linear search can be applied on both sorted and unsorted lists. Binary search can only be applied on sorted lists.
e) What will be the output of the following code?
float x = 7.87F;
System.out.println(Math.ceil(x));
System.out.println(Math.floor(x));
8.0
7.0

Question 3

a) State the difference between if-else-if ladder and switch case.
The if-else-if ladder is a bi-directional conditional statement. It can do a variety of comparisons. The switch case is a multi-branching conditional statement. It can only compare for equality of values.
b) Explain the concept of constructor with an example.
A constructor is a method in class with the same name as the class name. It is used to create objects and initialize them with legal initial values.
Example:

class Rectangle{
    int length;
    int breadth;
    public Rectangle(int a, int b){
        length = a;
        breadth = b;
    }
}

c) What will be the output of the following program segments?
i) String s = "application";
int p = s.indexOf(\'a\');
System.out.println(p);
System.out.println(p + s);
0
0application
ii) String st = "PROGRAM";
System.out.println(st.indexOf(st.charAt(4)));
1

iii) int a = 0;
if(a > 0 && a < 20)
a++;
else
a--;
System.out.println(a);
-1
iv) int a = 5, b = 2, c;
if(a > b || a != b)
System.out.print(c + " " + a + " " + b);
7 6 1
v) int i = 1;
while(i++ <= 1){
i++;
System.out.print(i + " ");
}
System.out.print(i);
3 4
d) Differentiate between isUpperCase(char) and toUpperCase(char).
The isUpperCase(char) returns a boolean value whereas the toUpperCase(char) returns a char data type value.
e) What is the difference between a constructor and a member function of a class?
A constructor doesn’t have a return type (not even void). A function always has a return type.
f) What is the difference between a static member function and a member function which is not static?
A static member function is invoked without an object. A non-static function is invoked through an object.