Questions List
Browse all explanatory questions of this chapter.
[String Tokenizer]
Question:
What is the difference between the two methods,
nextToken() and hasMoreTokens()
of StringTokenizer class?
[Operation on File]
Question:
What is the difference between the following two statements?
1. FileWriter fw = new FileWriter(); 2. FileWriter fw = new FileWriter( , true);
[Recursion]
Question:
A student has written the following code. It is written to check whether a string is palindrome or not. However, the code is not giving the desired result when the parameter “MADAM” is passed to the method isPalindrome(). Analyse the code and find out the logical error.
public class PalindromeTesting
{
public static boolean isPalindrome(String word)
{
if(word.length() < 1)
{
return true;
}
else if(word.charAt(0) != word.charAt(word.length() - 1))
{
return false;
}
else
{
return isPalindrome(word.substring(0, word.length() - 1));
}
}
}
[Recursion]
Question:
Following method is a part of a class MyArray.
int check(int m[ ], int i)
{
if(i <= 0)
{
return 0;
}
return check(m, i-2) + m[i-1];
}
public static void main()
{
int m[ ] = {1, 2, 3, 4};
int ans = check(m, m.length);
System.out.println(ans);
}
(a) Predict the output of the code considering there is no compilation error. Show the working.
(b) Considering the if condition is changed to if(i < 0),
how will it affect the output?
[Computer Hardware]
Question:
Draw the logic circuit diagram for the simplified form of the following expression:
AB'C + AB'C + ABC
The circuit diagram should only contain:
- One AND gate
- One OR gate
- One NOT gate
Question Diagram:
Expression: AB'C + AB'C + ABC
Answer:
Step 1: Simplification
AB'C + AB'C + ABC = AB'C + ABC = A(B'C + BC) = AC
Simplified Expression:
AC
[Computer Hardware]
Question:
Following is the circuit diagram of a Full Adder. Redraw the circuit diagram by adding the missing logic gates.

[Computer Hardware]
Question:
Write the canonical Product-of-Sum (POS) expression for the Sum and Carry of a Full Adder.
[Computer Hardware]
Question:
A 2 to 4 decoder contains 2 inputs denoted by A1 and A0 and 4 outputs denoted by D0, D1, D2, D3. Write the minterm representations for D1 and D3.
[Packages]
Question:
Given the following code snippet:
package mine;
public class Greetings
{
public static void greet(String name)
{
System.out.println("Hello " + name +
", welcome to the world of programming");
}
}
import java.util.*;
import mine.*;
public class Trial
{
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("Give your name");
String n = sc.next();
Greetings.greet(n);
}
}
(a) Predict the output for n = "Abhay".
(b) Explain the difference between:
import java.util.*;
and
import mine.*;
[Packages]
Question:
State the purpose of the following keywords in Java:
(a) package
(b) import
[Packages]
Question:
Why is the lang package termed as the default package in Java?
[String]
Question:
Find the output of the following snippet:
String S1 = "AB", S2 = "BC";
for(int i = 0, j = 0; i < S1.length(); i++, j++)
{
System.out.println(S1.charAt(i) + S2.charAt(j));
}
[String]
Question:
How many String objects are created in the following snippet? Justify your answer.
String s1 = "Gujarat";
String s2 = new String("Gujarat");
String s3 = new String("Gujarat");
String s4 = "Gujarat";
[Arrays]
Question:
What is the length of the array int arr[],
if its first index is x and the last index is y?
[String]
Question:
Find the output:
System.out.println(
"RocKet".substring(0, 4)
.compareTo("Rock".replace('m', 'o'))
);
[String]
Question:
Write the Java statement to print the position of the 2nd occurrence of the letter 'I' within the word "MICHIGAN".
[String and Arrays]
Question:
Find the output of the following snippet:
void check1()
{
String x[] = {"Kolkata","Chennai","Delhi"};
String y[] = {"Kolkata","Chennai","Delhi"};
System.out.println(x.equals(y));
}
Answer:
false
[Computational Complexity and Big-O Notation]
Question:
Finding the greatest common divisor (GCD) of two positive integers results in the time complexity of O(log N), where N is the larger number of two inputs. Justify the statement.
Question:
A student had written the following method to display “Hello World” four times:
void displayMessage()
{
System.out.println("Hello World");
System.out.println("Hello World");
System.out.println("Hello World");
System.out.println("Hello World");
}
Later, the student modified the code using a loop:
void displayMessage()
{
int i = 1;
while(i <= 4)
{
System.out.println("Hello World");
i++;
}
}
Compare the above code snippets with respect to time complexity.
Question:
Finding the greatest common divisor (GCD) of two positive integers results in the time complexity of O(log N), where N is the larger number of two inputs. Justify the statement.
[Computational Complexity and Big-O Notation]
Consider the following snippet, calculate the time complexity:
double m = 1, n = 1;
for(int i = 0; i < x; i++)
{
m *= Math.random();
}
for(int j = 0; j < y; j++)
{
n *= Math.random();
}
Consider the following snippet, calculate the time complexity:
[Primitive Values, Wrapper Classes, Types and Casting]
Complete the code of TeacherA and name the technique used:
TeacherA:
int average = ( ____ )((m1 + m2 + m3) / 3);
TeacherB:
int average = Math.round((m1 + m2 + m3) / 3);
Complete the code of TeacherA and name the technique used:
int average = ( ____ )((m1 + m2 + m3) / 3);
TeacherB:
int average = Math.round((m1 + m2 + m3) / 3);
[Primitive Values, Wrapper Classes, Types and Casting]
While using Wrapper classes, we do not need to import any package. Give a reason.
While using Wrapper classes, we do not need to import any package. Give a reason.
[Primitive Values, Wrapper Classes, Types and Casting]
What gets printed?
char[] set = {'*','D','5'};
System.out.print(Character.isDigit(set[2]) + " ");
System.out.print(Character.isWhitespace(set[0]) + " ");
System.out.print(Character.isLetterOrDigit(set[1]));
What gets printed?
System.out.print(Character.isDigit(set[2]) + " ");
System.out.print(Character.isWhitespace(set[0]) + " ");
System.out.print(Character.isLetterOrDigit(set[1]));
[Primitive Values, Wrapper Classes, Types and Casting]
Identify the most suitable data type (without losing precision and using memory efficiently) for:
(a) Population of a country
(b) Interest rate in a bank account
Identify the most suitable data type (without losing precision and using memory efficiently) for:
(a) Population of a country
(b) Interest rate in a bank account
[Primitive Values, Wrapper Classes, Types and Casting]
The following statement will generate a compile time error.
This can be rectified in 2 different ways, without losing precision of price.
Write these statements:
float price = 17.99;
The following statement will generate a compile time error. This can be rectified in 2 different ways, without losing precision of price. Write these statements:
[Primitive Values, Wrapper Classes, Types and Casting]
Since a byte can represent 256 different numbers,
why is its maximum value 127?
Since a byte can represent 256 different numbers, why is its maximum value 127?
[Primitive Values, Wrapper Classes, Types and Casting]
How many bytes of data is passed when
public void stu_data(int, double, char) is invoked?
How many bytes of data is passed when
public void stu_data(int, double, char) is invoked?
[Primitive Values, Wrapper Classes, Types and Casting]
Following is a code to convert a String into Double.
Fill the missing parts of the code:
________ str = "15.45";
double d = __________ (Double.parseDouble(str));
Following is a code to convert a String into Double. Fill the missing parts of the code:
[Polymorphism]
In the given code snippet, which feature of object‑oriented
programming concept is being exhibited by the
substring() method?
String s = "sandwich";
System.out.println(s.substring(4));
System.out.println(s.substring(1,4));
In the given code snippet, which feature of object‑oriented programming concept is being exhibited by the
substring() method?
[Inheritance]
Based on the given image, write the Java statement to create
the class Child.
Based on the given image, write the Java statement to create the class Child.
[Interface]
All methods declared in an interface are abstract.
Where will the body of these abstract methods be defined?
All methods declared in an interface are abstract. Where will the body of these abstract methods be defined?
[Inheritance]
What will happen if method
void show() in subclass
Car is given access specifier as
private?
What will happen if method
void show() in subclass
Car is given access specifier as
private?
[Inheritance]
What will happen if data members of the super class are made
private instead of protected?
What will happen if data members of the super class are made private instead of protected?
[Inheritance]
Study the given two classes below and identify the error, if any,
in the given code.
class Vehicle
{
protected String colour;
protected String registration;
public Vehicle(String c, String r)
{
colour = c;
registration = r;
}
public void show()
{
System.out.println("Colour: " + colour);
System.out.println("Registration number: " + registration);
}
}
class Car extends Vehicle
{
private double weight;
private String model;
public Car(String model, double weight,
String colour, String registration)
{
this.model = model;
this.weight = weight;
super(colour, registration);
}
public void show()
{
super.show();
System.out.println("Car Model name = " + model);
System.out.println("Car body weight = " + weight);
}
}
Study the given two classes below and identify the error, if any, in the given code.