Thursday 8 April 2021

Fascinating Number in JAVA

What is Fascinating Number?

Given a number N, the task is to check whether it is fascinating or not. 

Fascinating Number: When a number( 3 digits or more ) is multiplied by 2 and 3, and when both these products are concatenated with the original number, then it results in all digits from 1 to 9 present exactly once. There could be any number of zeros and are ignored. 

Examples: 

 


Input: 192 

Output: Yes 

After multiplication with 2 and 3, and concatenating with original number, resultant number is 192384576 which contains all digits from 1 to 9.

Input: 853 

Output: No 

After multiplication with 2 and 3, and concatenating with original number, the resultant number is 85317062559. In this, number 4 is missing and the number 5 has appeared multiple times.



import java.util.*;
class Fascinating
{
boolean isFascinating(int n)
{
String num = String.valueOf(n) + String.valueOf(n*2) + String.valueOf(n*3);
int count;
for(char i='1';i<='9';i++)
{
count = 0;
for(int j=0;j<num.length();j++)
{
if(num.charAt(j)==i)
{
count++;
}
}
if(count != 1)
{
return false;
}
}
return true;
}
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
Fascinating obj = new Fascinating();
int num;
System.out.println("Enter number here : ");
num = in.nextInt();
if(obj.isFascinating(num))
{
System.out.println(num+" is Fascinating Number");
}
else
{
System.out.println(num+" is not a Fascinating Number");
}
}
}

Wednesday 1 February 2017

Viva Questions for ISC Computer Practical Examination



Viva Questions for ISC Computer Practical Examination
Click Here to download this questions as pdf

Ø What is significance of import java.io.* in your program?
o A: The line imports all the classes of the java.io package into the current program.

Ø Give some examples of packages.
o A: java.util, java.lang etc.

Ø Why did you write java.util package? (only if you use classes like Scanner or StringTokenizer)
o A: To include the functions of the Scanner or StringTokenizer class from the java.util package.

Ø What is a class?
o A: A class is the blueprint from which individual objects are created.

Ø What is an object?
o A: An object is an instance of a class.

Ø Why do you write BufferedReader br = new ……. ?
o A: To activate the Buffer memory for efficient input and output operations. ‘br’ is an object of the BufferedReader class.

Ø Why do we have main() function?
o A: The execution of the program begins from the main() method.

Ø Why is the main method public?
o A: So that it be accessible to the JVM which begins to execute the program from outside of the class.

Ø Why is the main method static?
o A: So that it be available for execution without the need of any object.

Ø Is it compulsory to write String args[]?
o A: No it is not.

Ø Why do you write ‘throws IOEXception’?
o A: For handling any input/output exceptions.

Ø What are exceptions?
o A: Exceptions are runtime errors which prevent the program from working normally.

Ø Mention the two types of exceptions?
o A: Checked Exceptions – Exceptions which are checked (handled) during compile time.
o Example: IOException.
o Unchecked Exceptions – Exceptions which are not checked during compile time.
o Example: ArrayIndexOutOfBound.

Ø Mention other ways in which java handles exceptions.
o A: Java can handle exception using the try-catch block, throws keyword and throw keyword.

Ø What is the difference between throws and throw?
o A: Using throws keyword, we can give system defined error message if any error occurs, while using throw keyword, we can force an exception and give user-defined error messages.

Ø Name the primitive data-types in java.
o A: byte, short, int, long, float, double, char and boolean

Ø What are comments? Name the different types.
o A: Comments are statements which enhances the readability and understanding of the program. They are not part of the program.
o The different types are: single line (//….), multiple line (/* … */) and documenting comment (/**….*/).

Ø Why is the ‘S’ of System.out.println() function capital?
o A: System is the name of a class present in java,lang package and hence it begins with a capital letter.

Ø Why is the ‘S’ of String capital?
o A: Since String is a class.

Ø What is a variable?
o A: A variable is a named memory location whose value can change.

Ø What is a constant?
o A: A constant is a literal which cannot be changed.



Ø How do you make a variable into a constant?
o A: By adding the keyword ‘final’ before a variable declarations. Example: final int a = 5;

Ø What is the use of final keyword?
o A: Final can be used in three scenarios:
1.      final before a variable makes it a constant.
2.      final before a function declaration prevents it from being overridden.
             convention for class names.
3.      final before a class declaration prevents it from being inherited.

Ø What is a class variable?
o A: Instance variables having the keyword ‘static’ before it is a class variable. For every object there is just one copy of the variable made.

Ø Why do you write ‘Integer.parseInt(br.readLine())’?
o A: The inputs in a java program comes in the form of String objects which are read using the br.readLine() function. Now if we want the input in integer form, we have to convert it into integer using the parseInt() function of the Integer wrapper class.

Ø What are wrapper class?
o A wrapper class is a class which wraps (encloses) around a data type and gives it an object appearance. Wherever, the data type is required as an object, this object can be used.

Ø What is type conversion? Name its types.
o A: Converting a calue of a particular data type into another data-type is called type conversion. It is of two types:
1.      Implicit Type Conversion: When the conversion takes place on its own without the programmer’s intervention.
2.      Explicit Type Conversion: When the conversion takes place with the programmer’s intervention.

Ø What is the difference between if and switch?
o A:
1.      if can compare conditions for all data types whereas, switch can only check integers and characters.
2.      all kinds of relations can be checked using if whereas only equality relation can be checked using switch.

Ø What is the difference between for and while?
o A: The difference lies in the way they are commonly used. for loop is commonly used when the number of iterations are known whereas, while loop is commonly used when the number of iterations are not known.

Ø What is the difference between do-while and while?
o A: do-while lop is exit controlled (i.e. condition is checked at the exit) and runs at least once even if the condition is false whereas, while loop is entry controlled (i.e. condition is checked at the entry) and does not run even once if the condition is false.

Ø What is recursion?
o A: It is a process in which a function calls itself repeatedly until some base condition is satisfied.

Ø What is the difference between recursion and iteration?
o A: Recursion is usually slower than iteration due to overhead of maintaining stack, whereas, Iteration does not use stack so it’s faster than recursion.
Recursion uses more memory than iteration, whereas, Iteration consume less memory.
Recursion makes code smaller, whereas, Iteration makes code longer.

Ø What is the use of return keyword?
o A: return keyword is used to return any value from a function. It denotes the end of a function.

Ø Can there be multiple return statements in a function?
o A: Yes, but only one of them is executed.

Ø Can two functions have the same name? Give examples.
o A: Yes. In function overloading and function overriding.

Ø What is the difference between function overloading and function overriding?
o A: In function overloading only the function name is same but function signature (list of parameters) is different, whereas, in function overriding both the function name as well as function signature are same
Function overloading takes place within the same class, whereas, function overriding takes place in a child and a parent class.
Function overloading is an example of static polymorphism, whereas, function overriding is an example of dynamic polymorphism.

Ø What is a constructor?
o A: It is a member function with the same name as that of a class and is automatically called for initializing the variables of an object.

Ø What is the default access specifier?
o A: friendly

Ø What is the default java package?
o A: java.lang

Ø What is the use of ‘new’ keyword?
o A: It is used for dynamic memory allocation to reference data types.

Ø What is the use of ‘this’ keyword?
o A: It is used to refer to the current object (the object which calls the function).

Ø What are arrays?
o A: Arrays are a collection of variables of the same data-type referenced by a common name.

Ø What is the significance of arrays?
o A: It helps to group similar variables under a common name, hence reducing the number of names of variables we have to remember.

Ø What is StringTokenizer or Scanner and examples of similar classes (if you used it)
o A: StringTokenizer or Scanner is a class which splits up any given string into tokens separated by delimiters.

Ø Name some function of StringTokenizer class.
o A: nextToken(), countToken(), hasMoreTokens() etc.

Ø Name some function of Scanner class.
o A: next(), nextInt(), hasNextInt() etc.

Ø Name some other concepts related to Scanner/StringTokenizer
o A: Scanner class, StringTokenizer class, split() function.

Ø What is split() function?
o A: split() function is a function of the String class and it breaks up any String into tokens and outputs the result in the form of an array.

Ø What is the use of charAt() function?
o A: This function is used to extract characters at any given index from a String.

Ø What is the difference between length() and length?
o A: length() function is used to find the number of characters present in a String, whereas, length keyword is used to find the number if cells in an array.

Ø What is the difference between break and continue?
o A: break keyword stops the complete loop and takes the control out of the loop, whereas, the continue keyword just stops the current iteration and takes the control to the next iteration.

Ø What is the difference between selection sort and bubble sort?
o A: In selection sort, successive rounds are executed to select the element which is required to be placed in their sorted position, whereas, in bubble sort, every consecutive pairs of elements are compared and interchanged as required to place them in their sorted position.
      If we are arranging an array is ascending order, then in selection sort, we get the smallest element
      at every pass, whereas, in bubble sort we get the largest element in every pass.

Ø What is the drawback of an array?
o A: Its size cannot be changed.

Ø When does Binary search fail?
o A: When the array is not sorted.

Ø What is the difference between linear and binary search?
o A: Linear search does not require the array to be sorted, whereas, binary search requires that the array be sorted.
Linear search checks for the search item in a linear fashion from the beginning cell till the end, whereas, Binary search repeatedly dividing the array into halves and the search takes place in one of the halves. The element is searched in the middle cell of every half.
The Examiner may ask you to explain in brief the logic used you to solve the program.
You know what you have written so just give a brief summary of the logic used by you.
The Examiner may ask you to tell what is control variable in your loop.
So if your loop is for(int i = 1; i <= 5; i++) then the control variable is ‘i’.
The Examiner may ask you to tell what is the return type of a function you used.
So if your function is Boolean isPrime(int n) then the return type is ‘boolean’.

Ø What is a queue? Give a real life example.
o A: It is a linear data structure which follows the FIFO (First In First out) pattern.
Real life example: Queue at the ticket counter

Ø What is a stack? State it’s application.
o A: It is a linear data structure which follows the LIFO (Last In First out) pattern. Stack memory is used in recursion.
Application: It can be used for reversing strings.

All the best for your examinations
Regards
Shubham Kumar Sharma (sk.jsr.ind@gmail.com)
Sharma Tutorials Jamshedpur