Translate

DSA MT SOLVED

Q Explain queue with the help of an example. What are the types of operations that can be performed on a queue? How can you represent a queue in the form of a linked list? 


Ans
A queue is a list of elements in which items are inserted at one end of the queue and deleted from the other end of the queue. You can think of a queue as an open ended pipe, with elements being pushed from one end and coming out of another. The end at which elements are inserted is called the rear, and the end from which the elements are deleted is called the front. A queue is also called a First-In-First-Out (FIFO) list because the first element to be inserted in the queue is the first one to be deleted. The queue data structure is similar to the queues in real life.
The following two types of operations can be performed on queues:
•Insert: It refers to the addition of an item in the queue. Items are always inserted at the rear end of the queue.
•Delete: It refers to the deletion of an item from a queue. Items are always deleted from the front of the queue. When an item is deleted, the next item in the sequence becomes the front end of the queue.


class Node
{
public int data; public Node next;
}
class LinkedQueue
{
Node FRONT, REAR;
public LinkedQueue()
{
FRONT = null;
REAR = null;
}
public void insert (int element)
{
}
public void remove()
{
}
public void display()
{
}
}

Q Peter has been assigned a task to develop a code to implement a delete operation on a binary search tree. However, before doing so, he first needs to write a code to locate the position of the node to be deleted and its parent. To do so, he has written the following algorithm:
1. Make a variable/pointer currentNode point to the root node.2. Make a variable/pointer parent point to root.3. Repeat the steps, a, b, and c until currentNode becomes NULL or the value of the    node to be searched becomes greater than currentNode:    a. Make parent point to currentNode.    b. If the value to be deleted is greater than that of currentNode:         i. Make currentNode point to its left child.    c. If the value to be deleted is less than that of currentNode:         i. Make currentNode point to its right child.
However, the preceding algorithm does not give the desired output. Write the correct algorithm.


Ans
1. Make a variable/pointer currentNode point to the root node.
2. Make a variable/pointer parent point to NULL.
3. Repeat the steps, a, b, and c until currentNode becomes NULL or the value of the
    node to be searched becomes equal to that of currentNode:
    a. Make parent point to currentNode.
    b. If the value to be deleted is less than that of currentNode:
         i. Make currentNode point to its left child.
    c. If the value to be deleted is greater than that of currentNode:         
         i. Make currentNode point to its right child.

 Q.How can you represent a stack by using a linked list?Ans...


 Code in C#
class Node {
       public int info;
        public Node next;
public Node(int i, Node n)
{         
  info = i;
  next = n;
}}

After representing a node of a stack, you need to declare a class to implement operations on a stack. In this class, you also need to declare a variable/pointer to hold the address of the topmost element in the stack and initialize this variable/pointer to contain the value, NULL.
class Stack
   {   
    Node top;   
    public Stack() 
      {    
       top = null;
       }
       bool empty()
   {            // Statements        }
      public void push(int element)
        {            // Statements        }  
     public void pop()     
  {             // Statements        }}

After declaring the class to implement the operations on the stack, you need to implement the PUSH and POP operations.

Q. Define the following terms:a.    Edgeb.    Depth  of a treec.    Leaf noded.    Degree of a nodee.    Children of a node


Ans..
    Edge:  A link from a parent to a child node is known as an edge.
b.    Depth of a tree:  The maximum number of levels in a tree is called the depth of
       a tree.
c.    Leaf node: A node with no children is called a leaf node.
d.    Degree of a node: The number of sub trees of a node is called the degree of a
       node.
e.    Children of a node: The roots of the sub trees of a node are called the children of
       the node.


Q.  Explain stack with the help of an example. What are the characteristics of a stack? Briefly explain the operations that can be performed on a stack?


Ans.
A stack is a collection of data items that can be accessed at only one end, which is called top. This means that the items are inserted and deleted at the top. The last item that is inserted in a stack is the first one to be deleted. Therefore, a stack is called a Last-In-First-Out (LIFO) data structure.
A stack is like an empty box containing books, which is just wide enough to hold the books in one pile. The books can be placed, as well as removed, only from the top of the box. The book most recently put in the box is the first one to be taken out. The book at the bottom is the first one to be put inside the box and the last one to be taken out.
The characteristics of stacks are:
1.  Data can only be inserted on the top of the stack.
2.  Data can only be deleted from the top of the stack.
3.  Data cannot be deleted from the middle of the stack. All the items from the top
     first need to be removed.  
The following two basic operations can be performed on a stack:
1.  PUSH
2.  POP
When you insert an item into a stack, you say that you have pushed the item into the stack. When you delete an item from a stack, you say that you have popped the item from the stack.


Q. John has been assigned a task to develop a code to implement preorder traversal of a binary tree. He has developed the code according to the following algorithm:preorder (root)1. If (root = NULL):        a. Exit.2. preorder (left child of root).3. Visit (root).4. preorder (right child of root).

 Analyze the algorithm and check if the same will execute as expected. Suggest changes, if required. Also, write the algorithm for postorder traversal of a binary tree.


Ans.
The given algorithm is incorrect. The algorithm for preorder traversal of tree is:
preorder (root)
1. If (root = NULL):   
     a. Exit.
2. Visit (root).
3. preorder (left child of root).
4. preorder (right child of root).

The algorithm for postorder traversal of a tree is:
postorder (root)
1. If (root = NULL):   
     a. Exit.
2. postorder (left child of root).
3. postorder (right child of root).
4. Visit (root).

Q.  Explain stack with the help of an example. What are the characteristics of a stack? Briefly explain the operations that can be performed on a stack?


Ans.
A stack is a collection of data items that can be accessed at only one end, which is called top. This means that the items are inserted and deleted at the top. The last item that is inserted in a stack is the first one to be deleted. Therefore, a stack is called a Last-In-First-Out (LIFO) data structure.
A stack is like an empty box containing books, which is just wide enough to hold the books in one pile. The books can be placed, as well as removed, only from the top of the box. The book most recently put in the box is the first one to be taken out. The book at the bottom is the first one to be put inside the box and the last one to be taken out.
The characteristics of stacks are:
1.  Data can only be inserted on the top of the stack.
2.  Data can only be deleted from the top of the stack.
3.  Data cannot be deleted from the middle of the stack. All the items from the top
     first need to be removed.  
The following two basic operations can be performed on a stack:
1.  PUSH
2.  POP
When you insert an item into a stack, you say that you have pushed the item into the stack. When you delete an item from a stack, you say that you have popped the item from the stack.

Q.  Explain briefly how can you represent a binary tree?
Ans..

To represent a binary tree, you need to declare two classes:
•A class to represent a node of the tree: This class represents the structure of each node in a binary tree. A node in a binary tree consists of the following parts:
?Information: It refers to the information held by each node in a binary tree.
?Left child: It holds the reference of the left child of the node.
?Right child: It holds the reference of the right child of the node.


•A Class to represent the binary tree: This class implements various operations on a binary tree, such as insert, delete, and traverse. The class also provides the declaration of the variable/pointer root, which contains the address of the root node of the tree.

HTML MT Solved

Explain the usage of the <IFRAME> tag in HTML. Also, discuss the various attributes of the <IFRAME> tag.

Ans .The HTML <IFRAME> tag is used to specify an inline frame. It allows you to divide a Web page into sections or frames. Each section can be used to display an individual Web page. Therefore, the <IFRAME> tag is used to embed an HTML Web page within another Web page. The embedded Web page is said to be contained within the other Web page, which is known as the containing page. The following attributes can be used with the <IFRAME> tag:

src: Is used to specify the location or the URL of the Web page to be embedded inside the frame.

name: Is used to assign a name to the frame.

seamless: Is a boolean attribute, which instructs the browser to display the frame as a part of the containing Web page. If this attribute is used, the frame is displayed without scroll bars and border.

srcdoc: Is used to specify an HTML code that defines the content to be displayed inside the frame.

height: Is used to set the height of the frame.

width: Is used to set the width of the frame.


What do you mean by loop constructs? Discuss the various loop constructs in JavaScript.Ans. Loop constructs are used to repeatedly execute one or more lines of code. In JavaScript, the following loop constructs can be used:


while 
do...while
for
The while  loop is used to repeatedly execute a block of statements till a condition evaluates to true. The while statement always checks the condition before executing the statements in the loop. The syntax for the while loop is:

while (expression)
{
statements;
}

The do...while loop construct is similar to the while loop construct. However, the statements within the do...while loop are executed before the condition is checked as compared to the while loop, where the statements within the block are executed after the condition is checked. Therefore, the statements within the do...while loop are executed at least once, in comparison to the while loop, where the statements in the block are not executed when the condition evaluates to false for the very first time. It has the following syntax:

do
{ Statements;
}
while(condition)

The for  loop allows the execution of a block of code depending on the result of the evaluation of the test condition. It has the following syntax:

for (initialize variable; test condition; step value)
{// code block
}


Write the code to retrieve the users' location on the click event of a button by using the getCurrentPosition() method. The location coordinates that are retrieved need to be displayed inside the <P></P> tag. 

Ans. <!DOCTYPE HTML>
<HTML>
<BODY>
<P ID="disp_location">Click here to know your location coordinates:</P>
<BUTTON onclick="getLocation()">Get Location</Button>
<SCRIPT>
var geo=document.getElementById("disp_location");
function getLocation()
  {
  if (navigator.geolocation)
    {
    navigator.geolocation.getCurrentPosition(getPosition);
    }
  else{geo.innerHTML="Geolocation is not supported by this browser.";}
  }
function getPosition(position)
  {
  geo.innerHTML="Latitude: " + position.coords.latitude +
  "<BR>Longitude: " + position.coords.longitude;
  }
</SCRIPT>
</BODY>
</HTML>

What is a function? How can you create a function in JavaScript?

Ans. A function is a self-contained block of statements that has a name and is defined to perform a specific task. A function can be defined once but can be executed repeatedly. Functions can either be built-in or user-defined.

In JavaScript, functions can be created by using the keyword, function, followed by the function name and the parentheses, ().A function can optionally accept a list of parameters. The parameters that a function accepts are provided in parentheses and separated by commas. The function can use the values passed in these parameters to perform certain operations. The following syntax is used to create functions:

function [functionName] (Variable1, Variable2)
{
//function statements
}

Write the JavaScript code to create a clock that updates every one second to display the current time inside a <DIV> element on the Web page.

Ans. <!DOCTYPE HTML>
<HTML>
<HEAD>
<SCxRIPT type="text/javascript">
function clockTime()
{
var todayDate=new Date();
var hrs=todayDate.getHours();
var mns=todayDate.getMinutes();
var scs=todayDate.getSeconds();
mns=check(mns);
scs=check(scs);
document.getElementById('displayTime').innerHTML=hrs+":"+mns+":"+scs;
t=setTimeout('clockTime()',1000);
}

function check(t)
{
if (t<10)
 {
 t="0" + t;
 }
return t;
}
</SCRIPT>
</HEAD>
<BODY onload="clockTime()">
<DIV ID="displayTime"></DIV>
</BODY>
</HTML>

Define canvas. Also, discuss how can you create a canvas and use it for drawing graphic objects in HTML.Ans. A canvas is an area on a Web page that acts as a container for embedding graphic objects. It allows dynamic rendering of bitmap images and 2D shapes by using JavaScript. To create a canvas and use it for drawing, you need to perform the following tasks:


1. Define the canvas
2. Access the canvas

A canvas is defined by using the <CANVAS> tag in the body section of the HTML document. Its important attributes are height, width, style, and ID. You can define a canvas by using the following code within the <BODY> tag:

<CANVAS ID="myCanvas" width="300" height="300" style="border:1px solid black">
</CANVAS>

To actually draw graphic objects on the canvas, you need to access the canvas in the JavaScript code. You can write the following code in the <BODY> tag to access the canvas that you have defined earlier:

<SCRIPT>
var c=document.getElementByID("myCanvas");
var ctx=c.getContext("2d");
</SCRIPT>

Write a code to create a table in HTML, as shown in the following figure.

Ans. <!DOCTYPE HTML><HTML>
<BODY>
<TABLE border = "1">
<THEAD>
<TR><TH colspan= "4">Sales Record</TH></TR>
<TR> <TH> Product ID </TH> <TH> Description</TH> <TH> Quantity</TH><TH> Price</TH></TR>
</THEAD>
<TFOOT>
<TR><TD colspan= "3"> Total price </TD> <TD>$12</TD></TR>
</TFOOT>
<TBODY>
<TR><TD>101</TD><TD>Notebook</TD><TD>50</TD><TD>$5</TD></TR>
<TR><TD>121</TD><TD>Pen</TD><TD>100</TD><TD>$7</TD></TR>
</TBODY>
</TABLE>
</BODY>
</HTML>

What do you understand by preloading the images associated with a Web page? How is preloading of images implemented in JavaScript?

Ans. Preloading images is a technique to load images in the browser cache before the script on the Web page is executed and the Web page is rendered in the browser window. This helps to avoid any delay in loading images from their respective locations, and then displaying these images on the Web page.

The Image object can be used to preload images in an application. To use the Image object, an instance of the Image object needs to be created and the actual images need to be attached to the Image object in the head section. It ensures that the images are loaded in memory before the body of the page gets loaded. The following syntax is used to create an instance of the Image object:

var Imagename= new Image([Width], [Height]);

After instantiating an image object, you need to associate the image with the Image object. For this, the src attribute of the Image object is used as shown in the following code:
  
   Imagename.src="ImageURL",

where, ImageURL is the URL of the image.

What is the use of the <FIELDSET> tag in HTML? Also, briefly discuss the tags and attributes associated with the <FIELDSET> tag.

Ans. The <FIELDSET> tag is used to combine and group related fields in a form. It creates a box around the selected fields.

The <LEGEND> tag is used along with the <FIELDSET> tag to define a caption for the fieldset. It is the simplest way of organizing form elements along with their description in such a way that it is easier for a user to understand.

The <FIELDSET> tag can have the following attributes:

disabled: The disabled attribute is used to indicate that a group of fields should be shown disabled when the page loads.
form: The form attribute is used to specify the name of one or more forms to which the <FIELDSET> tag belongs.
name: The name attribute is used to specify the name for the fieldset.

 Write the code to create a canvas of desired dimension on a Web page and then draw a circle inside it. In addition, you need to apply a radial gradient comprising any four colors on the circle drawn on the canvas.

Ans. <!DOCTYPE HTML>
<HTML>   <BODY>
<CANVAS ID="myCanvas" width="300" height="300" style="border:1px solid black">
</CANVAS>
<SCRIPT>
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
var grad=ctx.createRadialGradient(75,50,5,90,60,100);
grad.addColorStop(0.2,"orange");
grad.addColorStop(0.4,"green");
grad.addColorStop(0.6,"yellow");
grad.addColorStop(0.8,"red");
ctx.fillStyle=grad;
ctx.beginPath();
ctx.arc(80, 90, 50, 0, Math.PI*2, false);
ctx.fill();
ctx.closePath();
</SCRIPT>
</BODY>
</HTML>

Core Java MT Solved


1.Identify the features of New API (NIO).

Ans:
i)The new API works more consistently across platforms.
ii)It makes it easier to write programs that gracefully handle the failure of file system operations.
iii)It provides more efficient access to a larger set of file attributes.

2.Differentiate between checked and unchecked exceptions.

Ans:
Checked Exception:
i)Every class that is a subclass of Exception except RuntimeException and its subclasses falls into the category of checked exceptions.
ii)You must ?handle or declare? these exceptions with a try or throws statement.

Unchecked Exception:
i)java.lang.RuntimeException and java.lang.Error and their subclasses are categorized as unchecked exceptions.
ii)You may use a try-catch statement to help discover the source of these exceptions, but when an application is ready for production use, there should be little code remaining that deals with RuntimeException and its subclasses.

3.Explain public, static, and void keywords in the following statement: public static void main(String args[])

Ans:
i)public: The public keyword indicates that the method can be accessed from anyobject in a Java program.
ii)static: The static keyword is used with the main() method that associates the method with its class. You need not create an object of the class to call the main() method.
iii)void: The void keyword signifies that the main() method returns no value.

4.Identify the limitations of the java.io.File class.

Ans:
The java.io.File class has the following limitations:
i)Many methods did not throw exceptions when they failed, so it was impossible to obtain useful error messages.
ii)Several operations were missing (file copy, move, and so on).
iii)The rename method did not work consistently across platforms.
iv)There was no real support for symbolic links.
v)More support for metadata was desired, such as file permissions, file owner, and other security attributes.
vi)Accessing file metadata was inefficient?every call for metadata resulted in a system call, which made the operations very inefficient.
vii)Many of the File methods did not scale. Requesting a large directory listing on a server could result in a hang.
viii)It was not possible to write reliable code that could recursively walk a file tree and respond appropriately if there were circular symbolic links.

5.Identify the five classes of the java.util.concurrent package and explain any two classes.

Ans:
i)Semaphore: Is a classic concurrency tool.
ii)CountDownLatch: A very simple yet very common utility for blocking until a given number of signals, events, or conditions hold.
iii)CyclicBarrier: A resettable multiway synchronization point useful in some styles of parallel programming.
iv)Phaser: Provides a more flexible form of barrier that may be used to control phased computation among multiple threads.
v)Exchanger: Allows two threads to exchange objects at a rendezvous point, and is useful in several pipeline designs.


6.Steve has been asked to automate the Library Management System either in C++ or Java. Steve has chosen to develop the project in Java. Identify the reason.

Ans:
One of the major problem areas in most of the object-oriented languages, such as C++, is to handle memory allocation. Programmers need to explicitly handle memory in the program for its optimum utilization. To handle memory allocation, they use pointers that enable a program to refer to memory location of the computer. However, Java does not support pointers and consists of the built-in functionality to manage memory.


7.You have created a class with two instance variables.You need to initialize the variables automatically when a class is initialized. Identify the method that you will use you to achieve this. In addition, describe the characteristics of this method.

Ans:
You can initialize the variable by using the constructor. The characteristics of a constructor are:
-       A constructor has the same name as the class itself.
-       There is no return type for a constructor. A constructor returns the instance of the class instead of a value.
-        A constructor is used to assign values to the data members of each objectcreated from a class

8.Differentiate between interface and abstract class.

Ans:
1.The methods of a Java interface are implicitly abstract and cannot have implementations. A Java abstract class can have instance methods that implements a default behavior.
2.Variables declared in a Java interface are by default final. An abstract class may contain non-final variables.
3.Members of a Java interface are public by default. A Java abstract class can have the usual flavors of class members like private, protected, etc..
4.Java interface should be implemented using keyword, implements; A Java abstract class should be extended using keyword, extends.
5.An interface can extend another Java interface only, an abstract class can extend another Java class and implement multiple Java interfaces.
6.A Java class can implement multiple interfaces but it can extend only one abstract class.
7.Interface is absolutely abstract and cannot be instantiated; A Java abstract class also cannot be instantiated, but can be invoked if a main() exists.
8.In comparison with Java abstract classes, Java interfaces are slow as it requires extra indirection.

Lock a Folder With Password Using Free Folder Protector

Lock a Folder With Password Using Free Folder Protector

All of us have some files and folders that we consider private. They can be anything from our business documents to pictures of friends and family. These are files that we don't want other people using our computer to know about. As Windows offers no way to protect our private information, most of us get in awkward situations when these files are discovered by unwanted people.

Lock Folder Windows
Surely, you can store this information in a hidden folder. The only problem is that anyone can easily search the contents of a hidden folder using Windows Search itself.

The only viable solution is storing this content in a password protected folder so that only those people knowing the password of the folder can access it. There are many folder locking programs available online but the problem is that most of the good ones are paid. Even if you do manage to get a good free one, you will be looked at suspiciously when people see a folder locker in the list of your installed programs.

If you too find yourself in a similar situation, you can try Folder Protector, a password protector for Windows folders that is not only free but is also portable meaning that it does not need to be installed. Just click on the exe file and the program will start running. I have created this program based on suggestions and feature requests that I have received by email over the last two years.

Folder Protector

Folder Protector offers each user a protected folder that can only be opened by entering the password in FolderProtector. Unlike most security programs, Folder Protector is small in size (nearly 58KB) and does not show the folder that it is protecting. This gives an additional advantage that people who don't know the password do not have a target to try and hack their way into. As the software is portable, you can hide it or even delete it after protecting your folder and no one will have a clue that it was used. Then whenever you need to access protected files, you can re-download the program from this page, enter the password and access your protected files.

Download Folder Protector

Download for Windows 8, Windows 7 and Windows Vista

Download for Windows XP


Important: 1) This program uses a simple method to lock files and should not be used to protect truly sensitive information. It is best to encrypt that kind of information.

2) Do not refresh your Windows when your folder is locked. Doing that will delete your files.

How to use Folder Protector?
When you run Folder Protector for the first time, the program will ask you for a password that you want to use. Enter a password that you can remember as this will be the password to your protected folder.

Lock Folder With Password

After entering the password, the program will open a folder named SecuredFILES. This is your protected folder. You can add all your private files in this folder. This folder is located at the Desktop. After adding all your files, you may close this folder.

Then, you can use the menu in the program to lock your folder. To lock the folder, type lock as your action. After successfully locking the folder, FolderProtector will display that the folder is locked.

Free Folder Lock

Unlocking the protected folder is easy. Just type unlock as your action. Then the program will ask you to enter your password. Upon successfully entering the password, the program will display the contents of your protected folder.

Changing the Password: Just type change as your action. The program will ask you for your current password. Upon successful entry of the current password, the program will ask for your new password and change it instantly.

Acknowledgment: This software has been made possible only through the suggestions of readers of this blog, especially Ernell Albert Galido for his idea of using a menu based interface.

Windows Compatibility: This program has been successfully tested on Windows 8.1, Windows 8, Windows 7, Windows Vista and Windows XP.

Support and Development
I will continue to develop this software further. So, your suggestions are invited. If you face any problem while using this software, you can use our support page.

12 Computer (PC) Tricks You Should Try Right Now

Computers have simplified our life to a great extent. Things that were impossible earlier can now be completed instantly thanks to computers. However, this does not mean that a PC is all work and no play.

Here are some of the best tricks you can try out on your Windows based computer.

Computer Tricks
  1. Have fun with Notepad
    If you think that Notepad is just a basic text editor, then, you will be amazed by its capabilities. You can use Notepad to create everything from personalized logs to harmless viruses that are incredibly annoying.Go see this post to know just how useful Notepad is.

  2. Command Prompt too has some tricks up its sleeves
    If you think that the Command prompt is a boring old program that no one uses, you are making a huge mistake. It can be used for everything from watching ASCII Star Wars to making folders that you cannot delete. See this post to know about all the cool stuff you can do with the Windows Command Prompt.

  3. Use Keyboard Shortcuts to get work done in no time
    If you are tired of having to alternate between your mouse and keyboard to operate your Windows computer, you would love to know these really useful keyboard shortcuts which greatly increase your speed and efficiency. See this post for details.

  4. Make your computer speak what you type
    You can use your PC's built in features and some VBScript magic to create a simple program that will make your computer speak whatever you input to it. Enter the right words and you could imitate a real conversation. Head over to this post to talk with your PC.

  5. Make your computer greet you every time you start Windows
    A simple modification in the previous trick will make your computer welcome you in its own mechanical voice every time you log onto Windows. This is achieved by placing the VBS script responsible for making your computer talk in the Start up folder. Read this post to have a computer said welcome.

  6. Find your computer's gender
    Want to know if your PC is a male or a female? Simple. Try the previous trick to know if your computer is a 'he' or a 'she'. On a serious note, this depends upon the voice you have selected in Microsoft Text to Speech options.

  7. Lock Folders with password
    If you have important personal files that you do not want other people to see, you can hide them in a password protected folder to prevent unwanted users from seeing them. Go see this post to hide your personal files effectively.

  8. Change your Processor's name
    PC Tricks
    If you are bored of your old processor and want a new one with a staggering name, you will definitely want to see this trick which allows you to change its name to something extraordinary to make your PC special.

  9. Make a Keyboard Disco
    You can use some VBScript coding to create a live disco on your keyboard by making the LED lights flash alternately. See this post to know how your keyboard can turn into a disco.

  10. Recover permanently deleted files in Windows
    If you have ever deleted a file in Windows that you did not want to and now want to recover it, you would definitely want to know about some free tools to recover your deleted files easily.

  11. Use your Keyboard as Mouse.
    You know you can use your mouse as keyboard using the On-screen keyboard utility. What if I tell you that it is also possible to do the reverse? Just read this post to see how.

  12. Disable USB ports to prevent others from taking your data
    Ever wanted to disable your USB ports to prevent others from using their flash drives on your PC? This post explains how to do just that with a simple registry trick. Do note that disabling USB ports will also disable your USB connected peripheral devices.

JAVA JOBS