Please select a subject first
Advertisements
Advertisements
State any one purpose of using the keyword this in Java programming.
Concept: Object as an Instance of a Class
State any one difference between instance variable and class variable.
Concept: Object as an Instance of a Class
Consider the following statement written in class Circle where pi is its data member.
static final double pi = 3.142;
Which of the following statements are valid for pi?
- It contains a common value for all objects class Circle.
- Its value is non-changeable.
- At a time two access modifiers, static and final, cannot be applied to a single data member pi.
Concept: Object as an Instance of a Class
The following function is a part of some class which is used to find the smallest digit present in a number. There are some places in the code marked by ?1?, ?2?, ?3? which must be replaced by an expression/a statement so that the function works correctly.
int small_dig(int n)
{ int min=?1?;
while(n!=0)
{
int q=n/10;
int r=?2?*10;
min=r>min ? ?3? : r;
n=q;
}
return min;
}
- What is the expression or statement at ?1?
- What is the expression or statement at ?2?
- What is the expression or statement at ?3?
Concept: Looping (For, While-do, Do-while, Continue, Break)
Design a class NumDude to check if a given number is a Dudeney number or not. (A Dudency number is a positive integer that is a perfect cube, such that the sum of its digits is equal to the cube root of the number.)
Example 5832 = (5 + 8 + 3 +2)3 = (18)3 = 5832
Some of the members of the class are given below:
| Class name | unique |
| Data member/instance variable: | |
| num | to store a positive integer number |
| Methods/Member functions: | |
| NumDude() | default constructor to initialise the data member with a legal initial value |
| void input() | to accept a positive integer number |
| int sumDigits(int x) | returns the sum of the digits of number 'x' using recursive technique |
| void isDude() | checks whether the given number is a Dudeney number by invoking the function sumDigits() and displays the result with an appropriate message. |
Specify the class NumDude giving details of the constructor ( ), void input( ), intsumDigits(int) and void is Dude(). Define a main() function to create an object and call the functions accordingly to enable the task.
Concept: Examples of Algorithmic Problem Solving Using Functions (Various Number Theoretic Problems, Finding Roots of Algebraic Equations)
A class Trans is defined to find the transpose of a square matrix. A transpose of a matrix is obtained by interchanging the elements of the rows and columns.
Example: If size of the matrix = 3, then
| ORIGINAL | ||
| 11 | 5 | 7 |
| 8 | 13 | 9 |
| 1 | 6 | 20 |
| TRANSPOSE | ||
| 11 | 8 | 1 |
| 5 | 13 | 6 |
| 7 | 9 | 20 |
Some of the member of the class are given below:
| Class name | Trans |
| Data members/instance variables: | |
| arr[ ] [ ] | to store integers in the matrix |
| m | integer to store the size of the matrix |
| Methods/Member functions: | |
| Trans(int mm) | parameterised constructor to initialise the data member m = mm |
| void fillarray( ) | to enter integer elements in the matrix |
| void transpose( ) | to create the transpose of the given matrix |
| void display( ) | displays the original matrix and the transposed matrix by invoking the method transpose( ) |
Specify the class Trans giving details of the constructor( ), void fillarray( ), void transpose( ) and void display( ). Define a main ( ) function to create an object and call the functions accordingly to enable the task.
Concept: Examples of Algorithmic Problem Solving Using Functions (Various Number Theoretic Problems, Finding Roots of Algebraic Equations)
int Toy(int n)
{ return (n<=0)? 1: n%10 + Toy(n/10); }
With reference to the program code given above, what will the function Toy() return when the value of n = 56?
Concept: Structured Data Types - Arrays (Single and Multi-dimensional), Strings
Write the statement in Java to extract the word “MISS” from the word “SUBMISSION”.
Concept: Example Algorithms that Use Structured Data Types (E.G. Searching, Finding Maximum/Minimum, Sorting Techniques, Solving Systems of Linear Equations, Substring, Concatenation, Length, Access to Char in String, Etc.)
What is the output of the statement given below?
System.out.print("FAN" + ("AUTOMATIC".charAt(5) ) );Concept: Example Algorithms that Use Structured Data Types (E.G. Searching, Finding Maximum/Minimum, Sorting Techniques, Solving Systems of Linear Equations, Substring, Concatenation, Length, Access to Char in String, Etc.)
Design a class Check which checks whether a word is a palindrome or not.
(Palindrome words are those which spell the same from either ends).
Example: MADAM, LEVEL etc.
The details of the members of the class are given below:
| Class name | Check |
| Data members/instance variables: | |
| wrd | stores a word |
| len | to store the length of the word |
| Methods/Member functions: | |
| Check( ) | default constructor |
| void acceptword( ) | to accept the word |
| boolean palindrome( ) | checks and returns ‘true’ if the word is a palindrome otherwise returns ‘false’ |
| void display( ) | displays the word along with an appropriate message |
Specify the class Check giving details of the constructor, void acceptword( ), boolean palindrome( ) and void display( ). Define the main( ) function to create an object and call the functions accordingly to enable the task.
Concept: Basic Input/Output Using Scanner and Printer Classes from JDK
Design a class Toggle which toggles a word by converting all upper case alphabets to lower case and vice versa.
Example: The word “mOTivATe” becomes “MotIVatE”
The details of the members of the class are given below:
| Class name | Toggle |
| Data members/instance variables: | |
| str | stores a word |
| newstr | stores the toggled word |
| len | to store the length of the word |
| Methods/Member functions: | |
| Toggle( ) | default constructor |
| void readword( ) | to accept the word |
| void toggle( ) | converts the upper case alphabets to lower case and all lower case alphabets to upper case and stores it in newstr |
| void display( ) | displays the original word along with the toggled word |
Specify the class Toggle giving details of the constructor, void readword( ), void toggle( ) and void display( ). Define the main( ) function to create an object and call the functions accordingly to enable the task.
Concept: Basic Input/Output Using Scanner and Printer Classes from JDK
If (~p ⇒ ~q), then its contrapositive will be ______.
Concept: Class as a Contract
Mention any two properties of the data members of an Interface.
Concept: Interfaces in Java
A matrix M[- 6, ... 10, 4 ... 15] is stored in the memory with each element requiring 4 bytes of storage. If the base address is 1025, find the address of M [4] [8] when the matrix is stored in Column Major wise.
Concept: Example Algorithms that Use Structured Data Types (E.G. Searching, Finding Maximum/Minimum, Sorting Techniques, Solving Systems of Linear Equations, Substring, Concatenation, Length, Access to Char in String, Etc.)
A class Sort Alpha has been defined to sort the words in the sentence in alphabetical order.
Example: Input: THE SKY IS BLUE
Output: BLUE IS SKY THE
Some of the members of the class are given below:
| Class name | Short Alpha |
|
Data members/instance variables: |
|
| sent |
to store a sentence |
| n | integer to store the number of words in a sentence |
|
Methods/Member functions: |
|
| ShortAlpha( ) |
default constructor to initialise data members with legal initial values |
| void acceptsent( ) | to accept a sentence in UPPERCASE |
| void short(SortAlpha P) | sorts the words of the sentence of object P in alphabetical order and stores the sorted sentence in the current object |
| void display( ) | display the original sentence along with the sorted sentence by invoking the method sort( ) |
Specify the class Sort Alpha giving details of the constructor (), void acceptsent(), void sort (Sort Alpha) and void display(). Define a main()function to create an object and call the functions accordingly to enable the task.
Concept: Example Algorithms that Use Structured Data Types (E.G. Searching, Finding Maximum/Minimum, Sorting Techniques, Solving Systems of Linear Equations, Substring, Concatenation, Length, Access to Char in String, Etc.)
State any one use of interfaces in Java.
Concept: Interfaces in Java
An array ARR [ -5 .....15, 10.....20] stores elements in Row Major Wise with each element requiring 2 bytes of storage. Find the address of ARR [10] [15] when the base address is 2500.
Concept: Structured Data Types - Arrays (Single and Multi-dimensional), Strings
Assertion: In Java, the String class is used to create and manipulate strings, and it is immutable.
Reason: Immutability ensures that once a String object is created, its value cannot be changed.
Concept: Structured Data Types - Arrays (Single and Multi-dimensional), Strings
A matrix M[-6….10, 4…15] is stored in the memory, with each element requiring 4 bytes of storage. If the base address is 1025, find the address of M[4][8] when the matrix is stored in column major-wise.
Concept: Example Algorithms that Use Structured Data Types (E.G. Searching, Finding Maximum/Minimum, Sorting Techniques, Solving Systems of Linear Equations, Substring, Concatenation, Length, Access to Char in String, Etc.)
The following function getIt() is a part of some class. Assume x is a positive integer, f is the lower bound of arr[ ] and l is the upper bound of the arr[ ].
Answer the questions given below along with dry run/working.
public int getIt(int x,intarr[],int f,int l)
{
if(f>l)
return-1;
int m=(f+l)/2;
if(arr[m]<x)
return getIt(x,m+1,l);
else if(arr[m]>x)
return getIt(x,f,m-1);
else
return m;
}
- What will the function getIt( ) return if arr[ ] = {10,20,30,40,50} and x = 40?
-
What is function getIt( ) performing apart from recursion?
Concept: Example Algorithms that Use Structured Data Types (E.G. Searching, Finding Maximum/Minimum, Sorting Techniques, Solving Systems of Linear Equations, Substring, Concatenation, Length, Access to Char in String, Etc.)
