In: Computer Science
Define for better understanding & give example. do not copy paste google answers of definitions.
1. What is the difference between a set and a list?
2. What is inheritance ?
3. Difference between a class and an object? How do classes relate to objecst?
4. How would you create an SQL table which makes use of customers information table and vehicle ownership over time? 5. How would you create a function that checks if a string is a palindrome?
1. Difference Between a Set and a List.
Set | List | |
1.Definition | Set is basically collection of elements, which is unordered list of elements, as order doesn't matters in a set. | List is an ordered sequence of elements. |
2.Duplicate Elements | Set also doesn't allows duplicate elements. For eg If you write {1,2,3,4,4,5} It is equivalent to {1,2,3,4,5}. Duplicate elements will be counted for once only. | List allows duplicate elements completely,It is allowed to store multiple duplicate elements in List. |
3.Null Elements | Set just allow one null element as there is no duplicate permitted while in Map you can have null values and at most one null key. | The list allows null elements and you can have many null objects in a List because it also allowed duplicates. |
4. Implementation & Uses | Set interface are implemented in HashSet, LinkedHashSet, and TreeSet | List is implemented in ArrayList, LinkedList, and Vector class. |
5. Order of elements |
Set doesn't maintains any order of elements {1,2,3,4,5} is equivalent to {2,3,4,5,1} |
List maintains order of elements as per they are inserted, hence we can easily access/insert/delete element easily at specified index. |
6. When to use | If the requirement is to have only unique values then Set is your best bet as any implementation of Set maintains unique values only. | If there is a need to maintain the insertion order irrespective of the duplicity then List is a best option |
Examples: I have shown implementation in JAVA.
Set: In this example i have taken a set ABC which have 5 values, Then I have shown implementation of this set Interface in Hash Set first and then in tree set, where hash set creates hashing and Treeset is ised to sort the elements.
import java.util.*;
import java.lang.*;
import java.io.*;
class Set
{
public static void main (String[] args) throws
java.lang.Exception
{
int ABC[] = {1,2,3,4,5};
Set<Integer> Hash = new HashSet<Integer>();
for(int i = 0; i<4; i++)
{
hset.add(ABC[i]);
}
System.out.println(Hash);
TreeSet<Integer> Tree = new
TreeSet<Integer>(Hash);
System.out.println("The sorted list is:");
System.out.println(Tree);
}
}
List: It is very simple to implement, I have shown Array
list implementation below
import java.util.List; import java.util.ArrayList; import java.util.LinkedList; public class List1 { public static void main(String[] args) { List<String> ABC = new ArrayList<String>(); ABC.add("Delhi"); ABC.add("Mumbai"); ABC.add("Bangalore"); System.out.println("List Elements: "); System.out.print(ABC); } }
2. Inheritance
First let's discuss types of classes:
Child Class:
The class that extends the features of another class is known as
child class, sub class or derived class.
Parent Class:
The class whose properties and functionalities are used(inherited)
by another class is known as parent class, super class or Base
class.
In Object oriented concepts, Inheritance is a Mechanism in which one object can acquire all the properties and behaviors of a parent object, by using extends keyword
Syntax of Inheritance:
class Subclass-name extends Superclass-name
{
//methods and fields
}
Thsi extends keyword shows that you are making a new child class that derives features from an existing Parent class. The meaning of "extends" is to increase the functionality, The most basic use of inheritance is to increase the code reusability and extend features in a class.
The biggest advantage of Inheritance is that only- the code which is already written in base (Parent) class that need not be rewritten in the child class, You can simply extend the child class from parent class it will itself gain the features.
There are various real life Inheritance examples like
3.) Difference between class and object
No. | Object | Class |
---|---|---|
1) | It is an instance of a class. We create instances of a class and we call them as objects of that class | Class is a blueprint or template from which objects are created. |
2) | It is like a real world entity such as pen, pencil etc. | Class is a group of similar objects, Many similar instances form a class. |
3) | Object is a physical entity. | Class is a logical entity. |
4) | Object is created through new keyword mainly e.g. Customer s1=new Customer); |
Class is declared using class keyword e.g. class Flower{} |
5) | Object is created many times as per requirement, we can create as many objects we need of a particular class. | Class is declared only once, Then we create multiple objects from it. |
6) | There are many ways to create object in java such as new keyword, newInstance() method, clone() method, etc. |
There is only one way to define class in java using class keyword. class classname { } |
4.) SQL TABLE for Customer Information
Since we have to maintain customer information table, we need some basic attributes like Customer Name, Customer Number can be considered as the primary key and rest we can have additional information like Age, Sex, Mobile No. Address etc.
And next to keep a record of ownership time we have to record date of purchase so that we can find the onwership time by calculating date difference from today's date.
CREATE TABLE Customer(Cust_No integer PRIMARY KEY, Cust_Name text , Cust_Age integer, Cust_Sex text , Purchase_Date DATE);
We have taken Customer Number as Integer which is Primary Key, Cust Name which is a text field, Customer Age which is integer and Cust Sex which is also text, and lastly Purchase_Date which is of DATE Data type. You can also use varchar or char instead of text.
Syantax for Date difference function:
DATEDIFF(interval, date1, date2)
5.) Palindrome - I have given a C function here.
#include <stdio.h>
#include <string.h>
int main()
{
char string[1000]; //Define a char array so as to take string
input
printf("Enter the string: ");
gets(string);
if(checkpalindrome(string))
printf("String is palindrome"); //If it gets 1
from function then palindrome
else
printf("String is not palindrome"); ////If it gets 0 from function
then Not palindrome
}
int checkpalindrome(char *string) //This function checks if it
is palindrome or not, It takes character pointer of the string
array
{
int i,a=0,n;
n=strlen(string); //strlen function returns length of the
string
for(i=0;i<n/2;i++)
{
if(string[i]==string[n-i-1]) //We check from reverse
and compare each character if it matches we increment a
a++;
}
if(a==i) //If after incrementing a it becomes
equals to i that means each and every element is same from
reverse
return 1; //If it returns 1 that means yes it is palindrome
else
return 0; //If returns 0 that means not palindrome
}
Output:
Note: I have mentioned comments in code and examples wherever necessary, Copy this code, save it with .c extension and run, It will work fine,
Hope it Helps..!!
I have tried to answer all 5 questions as nicely i could.