C Programs Tutorials | IT Developer
IT Developer

Java Programs



Share with a Friend

Short Questions/Answers - Java Programs

Question 1

a) Why is a class called a factory of objects?
A class is called a factory of objects because with one class definition, we can create several objects, each with their own state and behavior.
b) State the difference between a boolean literal and a character literal.
A boolean literal occupies 1 byte of storage, whereas a character literal occupies 2 bytes of storage. A boolean literal can store either true or false, whereas a character literal can store Unicode characters.
c) What is the use and syntax of a ternary operator?
A ternary operator is an alternative for if-else statement. It works with three operands.
Following is the syntax:
variable = (condition)? value 1 : value 2;
d) Write one word answer for the following:
(i) A method that converts a string to a primitive integer data type.
Integer.parseInt()
(ii) The default initial value of a boolean variable data type.
false
e) State one similarity and one difference between while and for loop.
Similarity: Both while and for loops are entry-controlled loops.
Difference: The while loop is used when the number of iterations is not known, whereas a for loop is used when the number of iterations is known.

Question 2

a) Write the function prototype for the function “sum” that takes an integer variable x as its argument and returns a value of float data type.
float sum(int x)
b) What is the use of the keyword this?
The keyword this is used to refer to the currently calling object.
c) Why is a class known as composite data type?
A class is known as a composite data type because it is generally user-defined, and it is built using one or more primitive data types.
d) Name the keyword that:
(i) is used for allocating memory to an array.
new
(ii) causes the control to transfer back to the method call.
return
e) Differentiate between pure and impure functions.
A pure function does not change the state of an object, whereas an impure function changes the state of an object.

Question 3

a) Write an expression for:

Math.pow(a + b, n) / (Math.sqrt(3) + b)
b) The following is a segment of a program:

x = 1; y = 1;
if(n > 0)
{
    x = x + 1;
    y = y - 1;
}

What will be the value of x and y, if n assumes a value (i) 1 (ii) 0?
(i) x = 2, y = 0
(ii) x = 1, y = 1
c) Analyze the following program segment and determine how many times the body of the loop will be executed (show the working):

x = 5; y = 50;
while(x <= y)
{
    y = y / x;
    System.out.println(y);
}

y = 50 –> y = 10 –> y = 2
The loop body executes 2 times.
d) When there are multiple definitions with the same function name, what makes them different from each other?
The number of arguments.
The data types of each argument.
e) Given that int x[][] = {{2, 4, 6}, {3, 5, 7}};
What will be the value of x[1][0] and x[0][2]?
x[1][0] = 3
x[0][2] = 6

f) Give the output of the following code fragment when (i) opn = ‘b’ (ii) opn = ‘x’ (iii) opn = ‘a’:

switch(opn)
{
    case \'a\':
    System.out.println("Platform Independent");
    break;
    case \'b\':
    System.out.println("Object Oriented");
    case \'c\':
    System.out.println("Robust and Secure");
    break;
    default:
    System.out.println("Wrong Input");
}

(i) When opn = ‘b’, the output is:
Object Oriented
Robust and Secure
(ii) When opn = ‘x’, the output is:
Wrong Input
(iii) When opn = ‘a’, the output is:
Platform Independent

g) Consider the following code and answer the questions that follow:

class academic
{
    int x, y;
    void access()
    {
        int a, b;
        academic student = new academic();
        System.out.println("Object created");
    }
}

(i) What is the object name of class academic?
The object name is student.
(ii) Name the class variables used in the program.
The class variables are x and y.
(iii) Write the local variables used in the program.
The local variables are a and b.
(iv) Give the type of function used and its name.
The type of function is void. It is used to indicate that the function doesn’t return any value.
h) Convert the following segment into an equivalent do loop:

int x, c;
for(x = 10, c = 20; c > 10; c = c - 2)
    x++;
int x = 10, c = 20;
while(c > 10){
    c = c - 2;
    x++;
}