Skip to main content

Posts

Questions on Inheritance in Java

Here are some objective question on Inheritance and interfaces  in Java. Which of the following statements are true about an abstract class? An abstract class can be instantiated  An abstract class can be subclassed An abstract class can not have a concrete method All of the above Which of the following is the correct declaration of an interface? interface Test { public void print(){}}      interface Test { public void print(); } interface Test {   int a; void print(){}} All of the above Which of the following statements are true? A final class can not be extended An abstract class can not be instantiated An interface can not be instantiated All of the above   What is the correct way of defining a class ABC which uses an interface DEF? class ABC extends DEF class DEF extends ABC  class ABC implements DEF None of the above   Method overriding is used to implement ------ Inherit...

Find nth node from end of list

Question : Write a program to find nth node from end of the list. A simple solution would be - traverse the list and find its length. And then go to length-n th node. But a better and more efficient solution is  (I got the idea- solution from geeksforgeeks website) Take two nodes nd1 and nd2 Move nd1 to nth node from beginning of list Set nd2 to first node Move nd1 and nd2 one node at a time in a loop until nd1 is last node (nd1.next is null) Now nd2 is the nth node from end of list  Here is complete code in Java. import java.util.Scanner ; class LinkedList { class Node { int num ; Node next ; public Node ( int n ){ num = n ; } Node getNext (){ return next ; } int getNum (){ return num ; } } public LinkedList (){ createList (); } public void createList () { int n ; Scanner scanner = new Scann...

Linked List in Java

Let us write a linked list program in Java. You certainly ask me why don't you use List class - either ArrayList or LinkedList class in Collections? Because your interviewer will not ask you about library classes. He wants you to write your own code. OK, the basic premise of linked list is that - it has a node and it is linked to next node with a pointer - or a reference in Java. So we can define a Node class as follows class Node{     int num;     Node next; } The data of the node can be integer or some complex structure. But we must have a reference to next node - which is the second field in the class. First node has reference of second node. Second node has reference of third node and so on. What about last node? What will be next of the last node? Last node will have next field as null. null indicates the termination of list.  Now the list also needs the reference of first node - head, first or you call it by any name. It ma...

Convert to Roman Number

The website leetcode has quite a huge collection of difficult coding questions. As they say, start small. I tried a easy question (medium according to the site). Converting a number to Roman numerals. We all know that in Roman number system M - 1000 D - 500 C - 100 L - 50 X - 10 V - 5 I - 1 And     CD is 400     CM is 900     XL is 40     XC is 90     IV is 4     IX is 9 If I look at many examples, it appears that 4 is never IIII, but IV. Similarly 40 is XL. So I think a symbol never appears 4 times, instead you take the next symbol and subtract it (by placing a symbol to its left). So here is question. And my algorithm was brute force - it was OK, because the question said that the numbers are between 1 to 3999. What I did was     Find the number of thousands and place M for each thousand     Number is number modulo 1000 Find the number ...

Covariant methods

Normally when we override a function(method) in a derived class, this overriding function must have the same signature as the function in base class. But in Java, the overriding method can have a different return type provided this return type is the sub-class of return type in overridden method. Such methods are called Covariant methods. e.g class Ret1{....} class B {     Ret1 foo(int n){       .......     } } class Ret2 extends Ret1{.....} class C extends B{      Ret2 foo(int n){         ....     } } In the derived class C, we are overriding the function foo. Return type of foo() in base class is Ret1 and return type of foo() in derived class is Ret2. This is valid because, Ret2 is a sub-class of Ret1. Let us look at a complete example. class Number { Number ( int n ) { this . n = n ; } int n ; } clas...

Reading values from Console in Java

How do you read a number from keyboard in Java ? Such a simple question to ask ? Or is it? Funnily reading values from console in Java is not as easy as scanf() or cin in Java Let us write the code import java.util.Scanner ; public class ReadNumber { public static void main ( String args []){ Scanner sc = new Scanner ( System . in ); int num = sc . nextInt (); int num2 = sc . nextInt (); System . out . println ( "Second number is " + num2 ); System . out . println ( "The number is " + num ); } } You need to use the class Scanner and call its constructor with System.in. Then you have to call various methods to read like scanner.nextInt(), scanner.nextFloat() etc. I remember writing programs using BufferedInputReader. Which was quite lengthy and needed throw block. Compared to input, displaying the output in Java is quite simple. You just need to use System.out.println...

Linux Command Help - Linux Quizard

So do you want to know about the commands beyond cp, rm and mkdir? Do you want to know what does file command do? Do you want to know what is whatis? Download the Linux Quizard app by Hegdeapps now and view through 244 MCQ on various topics.