tag:blogger.com,1999:blog-49502330720276246592009-02-21T07:41:39.421-08:00C,C++,JAVA,DataStructures,OS,.NET puzzlesThis blog has puzzles related to C,C++,Operating Systems,Java,.NET and Data Structurestechniehttp://www.blogger.com/profile/13608653955259756995noreply@blogger.comBlogger7125tag:blogger.com,1999:blog-4950233072027624659.post-42674578872855467672008-05-27T00:20:00.000-07:002008-05-27T00:33:57.673-07:00.NET1.Differences between DLL and EXE? <br /><hr><hr><br />2.Can an assembly have EXE? <br /><hr><hr><br />3.Can a DLL be changed to an EXE? <br /><hr><hr><br />4.Compare & contrast rich client (smart clients or Windows-based) & browser-based Web application <br /><hr><hr><br />5.Compare Client server application with n-Tier application <hr><hr><br />6.Can a try block have more than one catch block? <hr><hr><br />7.Can a try block have nested try blocks? <hr><hr><br />8.How do you load an assembly at runtime?<hr><hr> <br />9.If I am writing in a language like VB or C++, what are the procedures to be followed to support .NET? <hr><hr><br />10.How do you view the methods and members of a DLL? <hr><hr><br />11.What is shadowing? <hr><hr><br />12.What are the collections you’ve used? <hr><hr><br />13.State True or False: If you set AutoGenerateColumns=True and still provide custom column definitions, the DataGrid will render both <br />a)True<br />b)False<br /><hr><hr><br />14.The data from an XSL Transform with XmlReader can be returned in one of the following ways <br />a)objReader = objXslT.Transform(objNav, nothing)<br />b)objXslT.Transform(objNav, nothing)<br />c)objReader = objXslT.Transform(objNav, nothing, objWriter)<br />d)objXslT.Transform(objNav, nothing, objWriter)<br /><hr><hr><br /><br />15.Pick the command line that would result in the C# compiler generating an XML documentation file <br />a)csc /doc:NewHome.xml NewHome.cs<br /><br />b)c /doc /docfile: NewHome.xml NewHome.cs<br />c)csc /doc /out: NewHome.xml NewHome.cs<br /><br />d)csc /xml NewHome.cs<br /><hr><hr><br /><br />16.What is the comment syntax for C#’s XML-based documentation? <br />a)/** and **/<br />b)//#<br />c)///<br />d)//*<br /><hr><hr><br />16.When creating a C# Class Library project, what is the name of the supplementary file that Visual Studio.NET creates that contains General Information about the assembly? <br />a)AssemblyInfo.xml<br /><br />b)AssemblyInfo.cs<br />c)AssemblyInformation.cs<br /><br />d)AssemblyAttributes.cs<br /><hr><hr><br />17.Which of the following is the C# escape character for Null? <br />a)\n<br />b)\0<br />c)\f<br />d)\v<br /><hr><hr><br />18.What is the exception that is thrown when there is an attempt to dynamically access a method that does not exist? <br />a)MissingMethodException<br />b)TypeLoadException<br />c)MethodLoadException<br />d)MethodAccessException<br /><hr><hr><br />19.What method(s) must be used with the Application object to ensure that only one process accesses a variable at a time? <br />a)Synchronize()<br />b)Lock() and UnLock()<br />c)Lock()<br />d)Asynchroize()<hr><hr><br />20.After capturing the SelectedIndexChanged event for a ListBox control, you find that the event handler doesn’t execute. What could the problem be? <br />a)The AutoEventWireup attribute is set to False<br />b)The AutomaticPostBack attribute is set to False<br />c)The codebehind module is not properly compiled<br />d)The ListBox must be defined WithEvents<hr><hr><br />21.What method must be overridden in a custom control? <br />a)The Paint() method<br />b)The Control_Build() method<br /><br />c)The Render() method<br />d)The default constructor<br /><hr><hr><br />22.What is used to validate complex string patterns like an e-mail address? <br />a)Extended expressions<br />b)Regular expressions<br />c)Irregular expressions<br />d)Basic expressions<br /><hr><hr><br />23.The following is a valid statement in ASP.NET<%@ Page Language="C" %> <br />a)True<br />b)False<br /><hr><hr><br />24.A valid comment block in ASP.NET is <br />a)<!- - - Comment - - - ><br />b)<!- - Comment - - ><br />c)<% - - Comment - - %><br />d)<% ! - - Comment - - ><br /><hr><hr><br />25.The event handlers that can be included in the Global.asax file are <br />Application Start and<br />a)Session Start event handlers only<br />b)Application End and<br />c)Session End event handlers only<br />d)Per-request and Non-deterministic event handlers only<br />e)Application Start and End ,<br />f)Session Start and End, Per-request and Non-deterministic event handlers<br /><hr><hr><br />26.A Few of the Namespaces that get imported by default in an ASPX file are <br />a)System, System.Data, System.Drawing,<br />b)System.Globalization<br /><br />c)System,<br />System.IO, System.Management, System.Globalization<br />d)System, System.Collections,<br />e)System.Text, System.Web<br />f)System,<br />System.NET,<br />System.Reflection, System.Web<hr><hr><br />27.The Assemblies that can be referenced in an ASPX file without using @Assembly Directive is <br />a)System.dll, System.Data.dll,<br />b)System.Web.dll, System.Xml.dll,<br /><br />c)System.dll,<br />d)System.Collections.dll, System.IO.dll<br />e)System.dll, System.Reflection.dll,<br />f)System.Globalization.dll,<br /><br />g)System.Drawing.dll, System.Assembly.dll,<br />h)System.Text.dll<br /><hr><hr><br />28.An .ASHX file contains the following <br />a)Code-behind that are used in the code<br />b)Server Controls that can be called from a code-behind file<br />c)HTTP handlers-software modules that handle raw HTTP requests received by ASP.NET<br />d)Contains normal ASP.NET code and can be used as an include file<br /><hr><hr><br />29.What is the output for the following code snippet:<br /><br />public class testClass<br />{<br /> public static void Main(string[] args)<br /> {<br /> System.Console.WriteLine(args[1]);<br /> }//end Main<br />}//end class testClassCompiler Error <br />a)Runtime Error <br />b)Hello C# world <br />c)None of the above <br /><hr><hr><br />30.One of the possible way of writing an ASP.NET handler that works like an ISAPI filter- that is, that sees requests and responses and modifies them also, is by, <br />a)writing a module that extends FormsAuthenticatonModule and using it<br />b)writing a component class that extends HttpModuleCollection and using it<br />c)writing an HTTP module-a Class that implements IhttpModule and registering it in d)d)Web.Config<br />e)All of the above<br /><hr><hr><br />31.The ASP.NET directive that lets you cache different versions of a page based on varying input parameters, HTTP headers and browser type is <br />a)@OutputCache<br />b)@CacheOutput<br />c)@PageCache<br />d)@CacheAll<br /><hr><hr><br /><br /><br />32.How can be the web application get configured with the following authorization rules <br />a)Anonymous users must not be allowed to access the application.<br />b)All persons except David and John must be allowed to access the application. <br />c)<authorization><deny users = "applicationname\David, applicationname\John" ><allow roles ="*"><deny roles = "?"></authorization> <br />d)<authorization><allow users ="*"><deny users = "applicationname\David; applicationname\John" ><deny users = "*"></authorization> <br />e)<authorization><deny users = "applicationname\David, applicationname\John" ><deny users = "?"><allow users ="*"></authorization><br />f)<authorization><allow users ="*"><deny users = "applicationname\David, applicationname\John" ></authorization> <br /><hr><hr><br />33.What will be the output of the following code snippet?<br /><br />using System;<br />class MainClass<br />{<br /> static void Main( )<br /> {<br /> new MainClass().Display( 3.56 );<br /> }<br /><br /> private void Display( float anArg )<br /> {<br /> Console.Write( “{0} {1}”, anArg.GetType(), anArg );<br /> }<br /><br /> double Display( double anArg )<br /> {<br /> Console.Write( “{0} {1}”, anArg.GetType(), anArg );<br /> return anArg;<br /> }<br /><br /> public decimal Display( decimal anArg )<br /> {<br /> Console.Write( “{0} {1}”, anArg.GetType(), anArg ); return anArg;<br /> }<br />}<br />a)System.Single 3.56 <br /><br />b)System.Float 3.56 <br /><br />c)System.Double 3.56 <br /><br />d)System.Decimal 3.56 <br /><hr><hr><div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4950233072027624659-4267457887285546767?l=technicalpuzzles.blogspot.com'/></div>techniehttp://www.blogger.com/profile/13608653955259756995noreply@blogger.com1tag:blogger.com,1999:blog-4950233072027624659.post-33714764380417410332008-05-26T23:32:00.000-07:002008-05-26T23:37:47.230-07:00JAVA1.How could Java classes direct program messages to the system console, but error messages, say to a file? <br />Answer: The class System has a variable out that represents the standard output, and the variable err that represents the standard error device.By default, they both point at the system console.This how the standard output could be re-directed: <br /><br />Stream st = new Stream(new FileOutputStream("output.txt")); System.setErr(st); System.setOut(st); <br /><br /><HR><HR><br />2.What's the difference between an interface and an abstract class? <br />Answer: An abstract class may contain code in method bodies, which is not allowed in an interface.With abstract classes, you have to inherit your class from it and Java does not allow multiple inheritance.On the other hand, you can implement multiple interfaces in your class. <br /><br /><hr><hr><br />3.Why would you use a synchronized block vs.synchronized method? <br />Answer: Synchronized blocks place locks for shorter periods than synchronized methods. <br /><br /><hr><hr><br />4.Explain the usage of the keyword transient? <br />Answer: This keyword indicates that the value of this member variable does not have to be serialized with the object.When the class will be de-serialized, this variable will be initialized with a default value of its data type (i.e.zero for integers). <br /><hr><hr><br /><br />5.How can you force garbage collection? <br />Answer: You can't force GC, but could request it by calling System.gc().JVM does not guarantee that GC will be started immediately. <br /><br /><hr><hr><br />6.How do you know if an explicit object casting is needed? <br />Answer: If you assign a superclass object to a variable of a subclass's data type, you need to do explicit casting.For example: Object a; Customer b; b = (Customer) a;<br /><hr><hr><br />7.When you assign a subclass to a variable having a supeclass type, the casting is performed automatically. <br /><br /><hr><hr><br />8.What's the difference between the methods sleep() and wait() <br />Answer: The code sleep(1000); puts thread aside for exactly one second.The code wait(1000), causes a wait of up to one second.A thread could stop waiting earlier if it receives the notify() or notifyAll() call.The method wait() is defined in the class Object and the method sleep() is defined in the class Thread. <br /><hr><hr><br /><br />9.Can you write a Java class that could be used both as an applet as well as an application? <br />Answer: Yes.Add a main() method to the applet. <br /><hr><hr><br /><br />10.What's the difference between constructors and other methods? <br />Answer: Constructors must have the same name as the class and can not return a value.They are only called once while regular methods could be called many times. <br /><hr><hr><br /><br />11.Can you call one constructor from another if a class has multiple constructors <br />Answer: Yes.Use this() syntax. <br /><hr><hr><br /><br />12.Explain the usage of Java packages. <br />Answer: This is a way to organize files when a project consists of multiple modules.It also helps resolve naming conflicts when different packages have classes with the same names.Packages access level also allows you to protect data from being used by the non-authorized classes. <br /><hr><hr><br /><br />13.If a class is located in a package, what do you need to change in the OS environment to be able to use it? <br />Answer: You need to add a directory or a jar file that contains the package directories to the CLASSPATH environment variable.Let's say a class Employee belongs to a package com.xyz.hr; and is located in the file c:\dev\com\xyz\hr\Employee.java.In this case, you'd need to add c:\dev to the variable CLASSPATH.If this class contains the method main(), you could test it from a command prompt window as follows: c:\>java com.xyz.hr.Employee <br /><hr><hr><br /><br />14.What's the difference between J2SDK 1.5 and J2SDK 5.0? <br />Answer: There's no difference, Sun Microsystems just re-branded this version. <br /><hr><hr><br /><br />15.What would you use to compare two String variables - the operator == or the method equals()? <br />Answer: I'd use the method equals() to compare the values of the Strings and the == to check if two variables point at the same instance of a String object. <br /><hr><hr><br /><br />16.Does it matter in what order catch statements for FileNotFoundException and IOExceptipon are written? <br />Answer: Yes, it does.The FileNoFoundException is inherited from the IOException.Exception's subclasses have to be caught first. <br /><hr><hr><br /><br />17.Can an inner class declared inside of a method access local variables of this method? <br />Answer: It's possible if these variables are final. <br /><hr><hr><br /><br />18.What can go wrong if you replace && with & in the following code: String a=null; if (a!=null && a.length()>10) {...} <br />Answer: A single ampersand here would lead to a NullPointerException. <br /><hr><hr><br /><br />19.What's the main difference between a Vector and an ArrayList <br />Answer: Java Vector class is internally synchronized and ArrayList is not. <br /><hr><hr><br /><br />20.When should the method invokeLater()be used? <br />Answer: This method is used to ensure that Swing components are updated through the event-dispatching thread. <br /><hr><hr><br /><br />21.How can a subclass call a method or a constructor defined in a superclass? <br />Answer: Use the following syntax: super.myMethod(); To call a constructor of the superclass, just write super(); in the first line of the subclass's constructor. <br /><hr><hr><br />22.What's the difference between a queue and a stack? <br />Answer: Stacks works by last-in-first-out rule (LIFO), while queues use the FIFO rule <br /><hr><hr><br /><br />23.You can create an abstract class that contains only abstract methods.On the other hand, you can create an interface that declares the same methods.So can you use abstract classes instead of interfaces? <br />Answer: Sometimes.But your class may be a descendent of another class and in this case the interface is your only option. <br /><hr><hr><br /><br />24.What comes to mind when you hear about a young generation in Java? <br />Answer: Garbage collection. <br /><br /><hr><hr><br />25.What comes to mind when someone mentions a shallow copy in Java? <br />Answer: Object cloning. <br /><hr><hr><br /><br />26.If you're overriding the method equals() of an object, which other method you might also consider? <br />Answer: hashCode() <br /><hr><hr><br /><br />28.You are planning to do an indexed search in a list of objects.Which of the two Java collections should you use: ArrayList or LinkedList? <br />Answer: ArrayList <br /><hr><hr><br /><br />29.How would you make a copy of an entire Java object with its state? <br />Answer: Have this class implement Cloneable interface and call its method clone(). <br /><hr><hr><br /><br />30.How can you minimize the need of garbage collection and make the memory use more effective? <br />Answer: Use object pooling and weak object references. <br /><br /><hr><hr><br />There are two classes: A and B.The class B need to inform a class A when some important event has happened.What Java technique would you use to implement it? <br />Answer: If these classes are threads I'd consider notify() or notifyAll().For regular classes you can use the Observer interface. <br /><br /><br />What access level do you need to specify in the class declaration to ensure that only classes from the same directory can access it? <br />Answer: You do not need to specify any access level, and Java will use a default package access level<div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4950233072027624659-3371476438041741033?l=technicalpuzzles.blogspot.com'/></div>techniehttp://www.blogger.com/profile/13608653955259756995noreply@blogger.com0tag:blogger.com,1999:blog-4950233072027624659.post-83309166700513373572008-05-26T23:05:00.000-07:002008-05-26T23:48:32.870-07:00C++1.class base<br /> {<br /> public:<br /> void baseFun(){ cout«"from base"«endl;}<br /> };<br /> class deri:public base<br /> {<br /> public:<br /> void baseFun(){ cout« "from derived"«endl;}<br /> };<br />void SomeFunc(base *baseObj)<br />{<br /> baseObj->baseFun();<br />}<br />int main()<br />{<br />base baseObject;<br />SomeFunc(&baseObject);<br />deri deriObject;<br />SomeFunc(&deriObject);<br />}<br />Answer: <br /><br />from base <br />from base<br />Explanation: <br /><br />As we have seen in the previous case, SomeFunc expects a pointer to a base class. Since a pointer to a derived class object is passed, it treats the argument only as a base class pointer and the corresponding base function is called.<br /><hr><hr><br />2.class base<br /> {<br /> public:<br /> virtual void baseFun(){ cout«"from base"«endl;}<br /> };<br /> class deri:public base<br /> {<br /> public:<br /> void baseFun(){ cout« "from derived"«endl;}<br /> };<br />void SomeFunc(base *baseObj)<br />{<br /> baseObj->baseFun();<br />}<br />int main()<br />{<br />base baseObject;<br />SomeFunc(&baseObject);<br />deri deriObject;<br />SomeFunc(&deriObject);<br />}<br />Answer: <br /><br />from base <br />from derived <br />Explanation: <br /><br />Remember that baseFunc is a virtual function. That means that it supports run-time polymorphism. So the function corresponding to the derived class object is called.<br /><hr><hr><br />3.void main()<br />{<br /> int a, *pa, &ra;<br /> pa = &a;<br /> ra = a;<br /> cout «"a="«a «"*pa="«*pa «"ra"«ra ;<br />}<br />Answer: <br /><br />Compiler Error: 'ra',reference must be initialized<br />Explanation: <br /><br />Pointers are different from references. One of the main differences is that the pointers can be both initialized and assigned, whereas references can only be initialized. So this code issues an error.<br /><hr><hr><br />4.const int size = 5;<br />void print(int *ptr)<br />{<br /> cout«ptr[0];<br />}<br /><br />void print(int ptr[size])<br />{<br /> cout«ptr[0];<br />}<br /><br />void main()<br />{<br /> int a[size] = {1,2,3,4,5};<br /> int *b = new int(size);<br /> print(a);<br /> print(b);<br />}<br />Answer: <br /><br />Compiler Error : function 'void print(int *)' already has a body<br />Explanation: <br /><br />Arrays cannot be passed to functions, only pointers (for arrays, base addresses) can be passed. So the arguments int *ptr and int prt[size] have no difference as function arguments. In other words, both the functoins have the same signature and so cannot be overloaded. <br /><hr><hr><br />5.class some{<br />public:<br /> ~some()<br /> {<br /> cout«"some's destructor"«endl;<br /> }<br />};<br /><br />void main()<br />{<br /> some s;<br /> s.~some();<br />}<br />Answer : <br /><br />some's destructor <br />some's destructor<br />Explanation: <br /><br />Destructors can be called explicitly. Here 's.~some()' explicitly calls the destructor of 's'. When main() returns, destructor of s is called again, hence the result.<br /><hr><hr><br />6.#include ‹iostream.h›<br /><br />class fig2d<br />{<br /> int dim1;<br /> int dim2;<br /><br />public:<br /> fig2d() { dim1=5; dim2=6;}<br /><br /> virtual void operator«(ostream & rhs);<br />};<br /><br />void fig2d::operator«(ostream &rhs)<br />{<br /> rhs «this->dim1«" "«this->dim2«" ";<br />}<br /><br />/*class fig3d : public fig2d<br />{<br /> int dim3;<br />public:<br /> fig3d() { dim3=7;}<br /> virtual void operator«(ostream &rhs);<br />};<br />void fig3d::operator«(ostream &rhs)<br />{<br /> fig2d::operator «(rhs);<br /> rhs«this -> dim3;<br />}<br />*/<br /><br />void main()<br />{<br /> fig2d obj1;<br />// fig3d obj2;<br /><br /> obj1 « cout;<br />// obj2 « cout;<br />}<br />Answer : <br /><br />5 6 <br />Explanation: <br /><br />In this program, the « operator is overloaded with ostream as argument. This enables the 'cout' to be present at the right-hand-side. Normally, 'cout' is implemented as global function, but it doesn't mean that 'cout' is not possible to be overloaded as member function.<br />Overloading « as virtual member function becomes handy when the class in which it is overloaded is inherited, and this becomes available to be overrided. This is as opposed to global friend functions, where friend's are not inherited.<br /><hr><hr><br />6.class opOverload{<br />public:<br /> bool operator==(opOverload temp);<br />};<br /><br />bool opOverload::operator==(opOverload temp){<br /> if(*this == temp ){<br /> cout«"The both are same objects\n";<br /> return true;<br /> }<br /> else{<br /> cout«"The both are different\n";<br /> return false;<br /> }<br />}<br /><br />void main(){<br /> opOverload a1, a2;<br /> a1= =a2;<br />}<br />Answer : <br /><br />Runtime Error: Stack Overflow<br />Explanation: <br /><br />Just like normal functions, operator functions can be called recursively. This program just illustrates that point, by calling the operator == function recursively, leading to an infinite loop. <br /><hr><hr><br />7.class complex{<br /> double re;<br /> double im;<br />public:<br /> complex() : re(1),im(0.5) {}<br /> bool operator==(complex &rhs);<br /> operator int(){}<br />};<br /><br />bool complex::operator == (complex &rhs){<br /> if((this->re == rhs.re) && (this->im == rhs.im))<br /> return true;<br /> else<br /> return false;<br />}<br /><br />int main(){<br /> complex c1;<br /> cout« c1;<br />}<br />Answer : <br /><br />Garbage value<br />Explanation : <br /><br />The programmer wishes to print the complex object using output re-direction operator,which he has not defined for his lass.But the compiler instead of giving an error sees the conversion function and converts the user defined object to standard object and prints some garbage value.<br /><hr><hr><br />8.class complex{<br /> double re;<br /> double im;<br />public:<br /> complex() : re(0),im(0) {}<br /> complex(double n) { re=n,im=n;};<br /> complex(int m,int n) { re=m,im=n;}<br /> void print() { cout«re; cout«im;} <br />};<br /><br />void main(){<br /> complex c3;<br /> double i=5;<br /> c3 = i;<br /> c3.print();<br />}<br />Answer : <br /><br />5,5 <br />Explanation : <br /><br />Though no operator= function taking complex, double is defined, the double on the rhs is converted into a temporary object using the single argument constructor taking double and assigned to the lvalue.<hr><hr><br /><hr><hr><br />9.void main()<br />{<br /> int a, *pa, &ra;<br /> pa = &a;<br /> ra = a;<br /> cout «"a="«a «"*pa="«*pa «"ra"«ra ;<br />}<br />Answer : <br /><br />Compiler Error: 'ra',reference must be initialized<br />Explanation : <br /><br />Pointers are different from references. One of the main differences is that the pointers can be both initialized and assigned, whereas references can only be initialized. So this code issues an error<br /><hr><hr><br />10.Determine the output of the 'C++' Codelet. <br />class base<br /> { <br /> public : <br /> out() <br /> { <br /> cout«"base "; <br /> } <br /> };<br /> class deri{ <br /> public : out() <br /> { <br /> cout«"deri "; <br /> } <br /> };<br /> void main()<br /> { deri dp[3];<br /> base *bp = (base*)dp;<br /> for (int i=0; i<3;i++)<br /> (bp++)->out();<br /> }<br /><hr><hr><br />11.Justify the use of virtual constructors and destructors in C++. <br />Each C++ object possesses the 4 member fns,(which can be declared by the programmer explicitly or by the implementation if they are not available). What are those 4 functions? <br /><hr><hr><br />12.What is wrong with this class declaration? <br />class something<br /> {<br /> char *str;<br /> public:<br /> something(){<br /> st = new char[10]; }<br /> ~something()<br /> {<br /> delete str; <br /> }<br /> };<br /><hr><hr><br />13.Inheritance is also known as -------- relationship. Containership as ________ relationship. <br /><hr><hr><br />14.When is it necessary to use member-wise initialization list (also known as header initialization list) in C++? <br /><hr><hr><br />15.Which is the only operator in C++ which can be overloaded but NOT inherited<br /><hr><hr><div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4950233072027624659-8330916670051337357?l=technicalpuzzles.blogspot.com'/></div>techniehttp://www.blogger.com/profile/13608653955259756995noreply@blogger.com0tag:blogger.com,1999:blog-4950233072027624659.post-53375774537646348692008-05-26T22:56:00.000-07:002008-05-26T23:04:11.623-07:00OPERATING SYSTEMS1.Explain the concept of Reentrancy. <br />Answer: <br /><br />It is a useful, memory-saving technique for multiprogrammed timesharing systems. A Reentrant Procedure is one in which multiple users can share a single copy of a program during the same period. Reentrancy has 2 key aspects: The program code cannot modify itself, and the local data for each user process must be stored separately. Thus, the permanent part is the code, and the temporary part is the pointer back to the calling program and local variables used by that program. Each execution instance is called activation. It executes the code in the permanent part, but has its own copy of local variables/parameters. The temporary part associated with each activation is the activation record. Generally, the activation record is kept on the stack. Note: A reentrant procedure can be interrupted and called by an interrupting program, and still execute correctly on returning to the procedure.<br /><hr><hr><br />2.Explain Belady's Anomaly. <br />Answer: <br /><br />Also called FIFO anomaly. Usually, on increasing the number of frames allocated to a process' virtual memory, the process execution is faster, because fewer page faults occur. Sometimes, the reverse happens, i.e., the execution time increases even when more frames are allocated to the process. This is Belady's Anomaly. This is true for certain page reference patterns.<br /><hr><hr><br />3.What is a binary semaphore? What is its use? <br />Answer: <br /><br />A binary semaphore is one, which takes only 0 and 1 as values. They are used to implement mutual exclusion and synchronize concurrent processes.<br /><hr><hr><br />4.What is thrashing? <br />Answer: <br /><br />It is a phenomenon in virtual memory schemes when the processor spends most of its time swapping pages, rather than executing instructions. This is due to an inordinate number of page faults.<hr><hr><br />5.List the Coffman's conditions that lead to a deadlock. <br />Answer: <br /><br />Mutual Exclusion: Only one process may use a critical resource at a time. <br />Hold & Wait: A process may be allocated some resources while waiting for others. <br />No Pre-emption: No resource can be forcible removed from a process holding it. <br />Circular Wait: A closed chain of processes exist such that each process holds at least one resource needed by another process in the chain.<br /><hr><hr><br />6.What are short-, long- and medium-term scheduling? <br />Answer: <br /><br />Long term scheduler determines which programs are admitted to the system for processing. It controls the degree of multiprogramming. Once admitted, a job becomes a process. Medium term scheduling is part of the swapping function. This relates to processes that are in a blocked or suspended state. They are swapped out of real-memory until they are ready to execute. The swapping-in decision is based on memory-management criteria. <br />Short term scheduler, also know as a dispatcher executes most frequently, and makes the finest-grained decision of which process should execute next. This scheduler is invoked whenever an event occurs. It may lead to interruption of one process by preemption.<br /><hr><hr><br />7.are turnaround time and response time? <br />Answer: <br /><br />Turnaround time is the interval between the submission of a job and its completion. Response time is the interval between submission of a request, and the first response to that request.<br /><hr><hr><br />8.What are the typical elements of a process image? <br />Answer: <br /><br />User data: Modifiable part of user space. May include program data, user stack area, and <br />programs that may be modified. <br />User program: The instructions to be executed. <br />System Stack: Each process has one or more LIFO stacks associated with it. Used to store <br />parameters and calling addresses for procedure and system calls. <br />Process control Block (PCB): Info needed by the OS to control processes.<br /><hr><hr><br />9.What is the Translation Lookaside Buffer (TLB)? <br />Answer: <br /><br />In a cached system, the base addresses of the last few referenced pages is maintained in registers called the TLB that aids in faster lookup. TLB contains those page-table entries that have been most recently used. Normally, each virtual memory reference causes 2 physical memory accesses-- one to fetch appropriate page-table entry, and one to fetch the desired data. Using TLB in-between, this is reduced to just one physical memory access in cases of TLB-hit.<br /><hr><hr><br />10.What is the resident set and working set of a process? <br />Answer: <br /><br />Resident set is that portion of the process image that is actually in real-memory at a particular instant. Working set is that subset of resident set that is actually needed for execution. (Relate this to the variable-window size method for swapping techniques.)<br /><hr><hr><br />11.When is a system in safe state? <br />Answer: <br /><br />The set of dispatchable processes is in a safe state if there exists at least one temporal order in which all processes can be run to completion without resulting in a deadlock.<br /><hr><hr><br />12.What is cycle stealing? <br />Answer: <br /><br />We encounter cycle stealing in the context of Direct Memory Access (DMA). Either the DMA controller can use the data bus when the CPU does not need it, or it may force the CPU to temporarily suspend operation. The latter technique is called cycle stealing. Note that cycle stealing can be done only at specific break points in an instruction cycle.<br /><hr><hr><br />13.What is meant by arm-stickiness? <br />Answer: <br /><br />If one or a few processes have a high access rate to data on one track of a storage disk, then they may monopolize the device by repeated requests to that track. This generally happens with most common device scheduling algorithms (LIFO, SSTF, C-SCAN, etc). High-density multisurface disks are more likely to be affected by this than low density ones. <hr><hr><br />14.What are the stipulations of C2 level security? <br />Answer: <br /><br />C2 level security provides for: <br />Discretionary Access Control <br />Identification and Authentication <br />Auditing <br />Resource reuse<br /><hr><hr><br />15.What is busy waiting? <br />Answer: <br /><br />The repeated execution of a loop of code while waiting for an event to occur is called busy-waiting. The CPU is not engaged in any real productive activity during this period, and the process does not progress toward completion.<hr><hr><br />16.Explain the popular multiprocessor thread-scheduling strategies. <br />Answer: <br /><br />Load Sharing: Processes are not assigned to a particular processor. A global queue of threads is maintained. Each processor, when idle, selects a thread from this queue. Note that load balancing refers to a scheme where work is allocated to processors on a more permanent basis. <br />Gang Scheduling: A set of related threads is scheduled to run on a set of processors at the same time, on a 1-to-1 basis. Closely related threads / processes may be scheduled this way to reduce synchronization blocking, and minimize process switching. Group scheduling predated this strategy. <br />Dedicated processor assignment: Provides implicit scheduling defined by assignment of threads to processors. For the duration of program execution, each program is allocated a set of processors equal in number to the number of threads in the program. Processors are chosen from the available pool. <br />Dynamic scheduling: The number of thread in a program can be altered during the course of execution.<br /><hr><hr><br />17.When does the condition 'rendezvous' arise? <br />Answer: <br /><br />In message passing, it is the condition in which, both, the sender and receiver are blocked until the message is delivered.<br /><hr><hr><br />18.What is a trap and trapdoor? <br />Answer: <br /><br />Trapdoor is a secret undocumented entry point into a program used to grant access without normal methods of access authentication. A trap is a software interrupt, usually the result of an error condition.<hr><hr><br />19.What are local and global page replacements? <br />Answer: <br /><br />Local replacement means that an incoming page is brought in only to the relevant process' address space. Global replacement policy allows any page frame from any process to be replaced. The latter is applicable to variable partitions model only.<br /><hr><hr><br />20.Define latency, transfer and seek time with respect to disk I/O. <br />Answer: <br /><br />Seek time is the time required to move the disk arm to the required track. Rotational delay or latency is the time it takes for the beginning of the required sector to reach the head. Sum of seek time (if any) and latency is the access time. Time taken to actually transfer a span of data is transfer time. <hr><hr><br />21.Describe the Buddy system of memory allocation. <br />Answer: <br /><br />Free memory is maintained in linked lists, each of equal sized blocks. Any such block is of size 2^k. When some memory is required by a process, the block size of next higher order is chosen, and broken into two. Note that the two such pieces differ in address only in their kth bit. Such pieces are called buddies. When any used block is freed, the OS checks to see if its buddy is also free. If so, it is rejoined, and put into the original free-block linked-list.<hr><hr><div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4950233072027624659-5337577453764634869?l=technicalpuzzles.blogspot.com'/></div>techniehttp://www.blogger.com/profile/13608653955259756995noreply@blogger.com0tag:blogger.com,1999:blog-4950233072027624659.post-81219118039507606572008-05-26T22:42:00.000-07:002008-05-26T22:55:44.540-07:001.What is data structure? <br />Answer: A data structure is a way of organizing data that considers not only the items stored, but also their relationship to each other. Advance knowledge about the relationship between data items allows designing of efficient algorithms for the manipulation of data. <br /><br /><hr><hr><br />2.List out the areas in which data structures are applied extensively? <br />Answer: The name of areas are:<br /><br /><br />Compiler Design, <br />Operating System, <br />Database Management System, <br />Statistical analysis package, <br />Numerical Analysis, <br />Graphics, <br />Artificial Intelligence, <br />Simulation <br /><hr><hr><br />3.What are the major data structures used in the following areas : RDBMS, Network data model & Hierarchical data model. <br />Answer: The major data structures used are as follows:<br /><br /><br />RDBMS - Array (i.e. Array of structures) <br />Network data model - Graph <br />Hierarchical data model - Trees <br /><br />4.If you are using C language to implement the heterogeneous linked list, what pointer type will you use? <br />Answer: The heterogeneous linked list contains different data types in its nodes and we need a link, pointer to connect them. It is not possible to use ordinary pointers for this. So we go for void pointer. Void pointer is capable of storing pointer to any type as it is a generic pointer type. <br /><hr><hr><br /><br />5.Minimum number of queues needed to implement the priority queue? <br />Answer: Two. One queue is used for actual storing of data and another for storing priorities. <br /><br /><hr><hr><br />6.What is the data structures used to perform recursion? <br />Answer: Stack. Because of its LIFO (Last In First Out) property it remembers its 'caller' so knows whom to return when the function has to return. Recursion makes use of system stack for storing the return addresses of the function calls.<br /><br />Every recursive function has its equivalent iterative (non-recursive) function. Even where such equivalent iterative procedures are written, explicit stack is to be used. <br /><hr><hr><br /><br />7.What are the notations used in Evaluation of Arithmetic Expressions using prefix and postfix forms?<br />Answer: Polish and Reverse Polish notations. <br /><hr><hr><br /><br />8.Convert the expression ((A + B) * C - (D - E) ^ (F + G)) to equivalent Prefix and Postfix notations. <br />Answer: Prefix Notation: ^ - * +ABC - DE + FG<br /><br />Postfix Notation: AB + C * DE - - FG + ^ <br /><hr><hr><br /><br />9.How many null branches are there in a binary tree with 20 nodes? <br />Answer: 21<br /><hr><hr><br /><br /><br /><br />10.What are the methods available in storing sequential files? <br />Answer: The methods available in storing sequential files are:<br /><br /><br />Straight merging, <br />Natural merging, <br />Polyphase sort, <br />Distribution of Initial runs. <br /><hr><hr><br /><br />11.How many different trees are possible with 10 nodes ?<br />Answer: 1014<br /><hr><hr><br /><br /><br /><br /><br />12.List out few of the Application of tree data-structure? <br />Answer: The list is as follows:<br /><br /><br />The manipulation of Arithmetic expression, <br />Symbol Table construction, <br />Syntax analysis. <br /><hr><hr><br />13.List out few of the applications that make use of Multilinked Structures? <br />Answer: The applications are listed below:<br /><br /><br />Sparse matrix, <br />Index generation. <br /><hr><hr><br />14.In tree construction which is the suitable efficient data structure? <br />Answer: Linked list is the efficient data structure. <br /><hr><hr><br /><br />15.What is the type of the algorithm used in solving the 8 Queens problem? <br />Answer: Backtracking<div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4950233072027624659-8121911803950760657?l=technicalpuzzles.blogspot.com'/></div>techniehttp://www.blogger.com/profile/13608653955259756995noreply@blogger.com0tag:blogger.com,1999:blog-4950233072027624659.post-28485392367078375542008-05-20T05:38:00.000-07:002008-05-23T05:04:03.547-07:00<span style="font-weight:bold;">C puzzles:
<br />
<br />1.<strong>#include<<strong>stdio.h</strong>></strong>
<br /> #define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
<br /> int array[] = {23,34,12,17,204,99,16};
<br />
<br /> int main()
<br /> {
<br /> int d;
<br />
<br /> for(d=-1;d <= (TOTAL_ELEMENTS-2);d++)
<br /> printf("%d\n",array[d+1]);
<br />
<br /> return 0;
<br /> }
<br />
<br />Find out what's going wrong.
<br />hint
<br />
<br />I thought the following program was a perfect C program. But on compiling, I found a silly mistake. Can you find it out (without compiling the program :-) ?
<br />
<br /><strong>2.#include<<strong>stdio.h</strong>></strong>
<br />
<br />void OS_Solaris_print()
<br />{
<br /> printf("Solaris - Sun Microsystems\n");
<br />}
<br />
<br />void OS_Windows_print()
<br />{
<br /> printf("Windows - Microsoft\n");
<br />
<br />}
<br />void OS_HP-UX_print()
<br />{
<br /> printf("HP-UX - Hewlett Packard\n");
<br />}
<br />
<br />int main()
<br />{
<br /> int num;
<br /> printf("Enter the number (1-3):\n");
<br /> scanf("%d",&num);
<br /> switch(num)
<br /> {
<br /> case 1:
<br /> OS_Solaris_print();
<br /> break;
<br /> case 2:
<br /> OS_Windows_print();
<br /> break;
<br /> case 3:
<br /> OS_HP-UX_print();
<br /> break;
<br /> default:
<br /> printf("Hmm! only 1-3 :-)\n");
<br /> break;
<br /> }
<br />
<br /> return 0;
<br />}
<br />
<br />hint
<br />What's the expected output for the following program and why?
<br />
<br />3.enum {false,true};
<br />
<br />int main()
<br />{
<br /> int i=1;
<br /> do
<br /> {
<br /> printf("%d\n",i);
<br /> i++;
<br /> if(i < 15)
<br /> continue;
<br /> }while(false);
<br /> return 0;
<br />}
<br />
<br />hint
<br />The following program doesn't "seem" to print "hello-out". (Try executing it)
<br />
<br />4. <strong>#include<<strong>stdio.h</strong>></strong>
<br /><strong>#include<<strong>unistd.h</strong>></strong>
<br /> int main()
<br /> {
<br /> while(1)
<br /> {
<br /> fprintf(stdout,"hello-out");
<br /> fprintf(stderr,"hello-err");
<br /> sleep(1);
<br /> }
<br /> return 0;
<br /> }
<br />
<br />What could be the reason?
<br />
<br />
<br /><strong>5. #include<<strong>stdio.h</strong>></strong>
<br /> #define f(a,b) a##b
<br /> #define g(a) #a
<br /> #define h(a) g(a)
<br />
<br /> int main()
<br /> {
<br /> printf("%s\n",h(f(1,2)));
<br /> printf("%s\n",g(f(1,2)));
<br /> return 0;
<br /> }
<br />
<br />Just by looking at the program one "might" expect the output to be, the same for both the printf statements. But on running the program you get it as:
<br />bash$ ./a.out
<br />12
<br />f(1,2)
<br />bash$
<br />
<br />Why is it so?
<br />hint
<br />
<br /><strong>6. #include<<strong>stdio.h</strong>></strong>
<br /> int main()
<br /> {
<br /> int a=10;
<br /> switch(a)
<br /> {
<br /> case '1':
<br /> printf("ONE\n");
<br /> break;
<br /> case '2':
<br /> printf("TWO\n");
<br /> break;
<br /> defa1ut:
<br /> printf("NONE\n");
<br /> }
<br /> return 0;
<br /> }
<br />
<br />If you expect the output of the above program to be NONE, I would request you to check it out!!
<br />The following C program segfaults of IA-64, but works fine on IA-32.<br><br><br>
<br />
<br /><script type="text/javascript"><!--
<br />google_ad_client = "pub-5049753950830338";
<br />/* 728x90, created 5/23/08 */
<br />google_ad_slot = "9098528025";
<br />google_ad_width = 728;
<br />google_ad_height = 90;
<br />//-->
<br /></script>
<br /><script type="text/javascript"
<br />src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
<br /></script> <br><br><br>
<br />7. int main()
<br /> {
<br /> int* p;
<br /> p = (int*)malloc(sizeof(int));
<br /> *p = 10;
<br /> return 0;
<br /> }
<br />
<br />Why does it happen so?
<br />Here is a small piece of program(again just 14 lines of program) which counts the number of bits set in a number.
<br />Input Output
<br />0 0(0000000)
<br />5 2(0000101)
<br />7 3(0000111)
<br />
<br /> int CountBits (unsigned int x )
<br /> {
<br /> static unsigned int mask[] = { 0x55555555,
<br /> 0x33333333,
<br /> 0x0F0F0F0F,
<br /> 0x00FF00FF,
<br /> 0x0000FFFF
<br /> } ;
<br />
<br /> int i ;
<br /> int shift ; /* Number of positions to shift to right*/
<br /> for ( i =0, shift =1; i < 5; i ++, shift *= 2)
<br /> x = (x & mask[i ])+ ( ( x >> shift) & mask[i]);
<br /> return x;
<br /> }
<br />
<br />Find out the logic used in the above program. What do you think would be the output of the following program and why? (If you are about to say "f is 1.0", I would say check it out again)
<br />
<br /><strong>8.#include <<strong>stdio.h</strong>></strong>
<br />int main()
<br />{
<br /> float f=0.0f;
<br /> int i;
<br />
<br /> for(i=0;i<10;i++)
<br /> f = f + 0.1f;
<br />
<br /> if(f == 1.0f)
<br /> printf("f is 1.0 \n");
<br /> else
<br /> printf("f is NOT 1.0\n");
<br />
<br /> return 0;
<br />}
<br />
<br />I thought the following C program is perfectly valid (after reading about the comma operator in C). But there is a mistake in the following program, can you identify it?
<br />
<br /><strong>9.#include <<strong>stdio.h</strong>></strong>
<br />int main()
<br />{
<br /> int a = 1,2;
<br /> printf("a : %d\n",a);
<br /> return 0;
<br />}
<br />
<br />What would be the output of the following C program? (Is it a valid C program?)
<br />
<br /><strong>10.#include <<strong>stdio.h</strong>></strong>
<br />int main()
<br />{
<br /> int i=43;
<br /> printf("%d\n",printf("%d",printf("%d",i)));
<br /> return 0;
<br />}
<br />
<br /> void duff(register char *to, register char *from, register int count)
<br /> {
<br /> register int n=(count+7)/8;
<br /> switch(count%8){
<br /> case 0: do{ *to++ = *from++;
<br /> case 7: *to++ = *from++;
<br /> case 6: *to++ = *from++;
<br /> case 5: *to++ = *from++;
<br /> case 4: *to++ = *from++;
<br /> case 3: *to++ = *from++;
<br /> case 2: *to++ = *from++;
<br /> case 1: *to++ = *from++;
<br /> }while( --n >0);
<br /> }
<br /> }
<br />
<br />Is the above valid C code? If so, what is it trying to acheive and why would anyone do something like the above? Here is yet another implementation of CountBits. Verify whether it is correct (how do you that???). If so, find out the logic used.
<br />
<br /> int CountBits(unsigned int x)
<br /> {
<br /> int count=0;
<br /> while(x)
<br /> {
<br /> count++;
<br /> x = x&(x-1);
<br /> }
<br /> return count;
<br /> }
<br />
<br />Are the following two function prototypes same?
<br />
<br /> int foobar(void);
<br /> int foobar();
<br />
<br />The following programs should be of some help in finding the answer: (Compile and run both the programs and see what happens)
<br />Program 1:
<br />
<br /> #include <stdio.h>
<br /> void foobar1(void)
<br /> {
<br /> printf("In foobar1\n");
<br /> }
<br />
<br /> void foobar2()
<br /> {
<br /> printf("In foobar2\n");
<br /> }
<br />
<br /> int main()
<br /> {
<br /> char ch = 'a';
<br /> foobar1();
<br /> foobar2(33, ch);
<br /> return 0;
<br /> }
<br />
<br />Program 2:
<br />
<br /> #include <stdio.h>
<br /> void foobar1(void)
<br /> {
<br /> printf("In foobar1\n");
<br /> }
<br />
<br /> void foobar2()
<br /> {
<br /> printf("In foobar2\n");
<br /> }
<br />
<br /> int main()
<br /> {
<br /> char ch = 'a';
<br /> foobar1(33, ch);
<br /> foobar2();
<br /> return 0;
<br /> }
<br />
<br />What's the output of the following program and why?
<br />
<br /> #include <stdio.h>
<br /> int main()
<br /> {
<br /> float a = 12.5;
<br /> printf("%d\n", a);
<br /> printf("%d\n", *(int *)&a);
<br /> return 0;
<br /> }
<br />
<br />The following is a small C program split across files. What do you expect the output to be, when both of them compiled together and run?
<br />File1.c
<br />
<br /> int arr[80];
<br />
<br />File2.c
<br />
<br /> extern int *arr;
<br /> int main()
<br /> {
<br /> arr[1] = 100;
<br /> return 0;
<br /> }
<br />
<br />Explain the output of the following C program (No, the output is not 20).
<br />
<br /> #include<stdio.h>
<br /> int main()
<br /> {
<br /> int a=1;
<br /> switch(a)
<br /> { int b=20;
<br /> case 1: printf("b is %d\n",b);
<br /> break;
<br /> default:printf("b is %d\n",b);
<br /> break;
<br /> }
<br /> return 0;
<br /> }
<br />
<br />What is the output of the following program? (Again, it is not 40, (if the size of integer is 4)).
<br />
<br /> #define SIZE 10
<br /> void size(int arr[SIZE])
<br /> {
<br /> printf("size of array is:%d\n",sizeof(arr));
<br /> }
<br />
<br /> int main()
<br /> {
<br /> int arr[SIZE];
<br /> size(arr);
<br /> return 0;
<br /> }
<br />
<br />The following is a simple c program, in which there is a function called Error to display errors. Can you see a potential problem with the way Error is defined?
<br />
<br /> #include <stdlib.h>
<br /> #include <stdio.h>
<br /> void Error(char* s)
<br /> {
<br /> printf(s);
<br /> return;
<br /> }
<br />
<br /> int main()
<br /> {
<br /> int *p;
<br /> p = malloc(sizeof(int));
<br /> if(p == NULL)
<br /> {
<br /> Error("Could not allocate the memory\n");
<br /> Error("Quitting....\n");
<br /> exit(1);
<br /> }
<br /> else
<br /> {
<br /> /*some stuff to use p*/
<br /> }
<br /> return 0;
<br /> }
<br />
<br />What is the differnce between the following function calls to scanf?(Please notice the space carefully in the second call. Try removing it and observe the behaviour of the program)
<br />
<br /> #include <stdio.h>
<br /> int main()
<br /> {
<br /> char c;
<br /> scanf("%c",&c);
<br /> printf("%c\n",c);
<br />
<br /> scanf(" %c",&c);
<br /> printf("%c\n",c);
<br />
<br /> return 0;
<br /> }
<br />
<br />What is the potential problem with the following C program?
<br />
<br /> #include <stdio.h>
<br /> int main()
<br /> {
<br /> char str[80];
<br /> printf("Enter the string:");
<br /> scanf("%s",str);
<br /> printf("You entered:%s\n",str);
<br />
<br /> return 0;
<br /> }
<br />
<br />What is the output of the following program?
<br />
<br /> #include <stdio.h>
<br /> int main()
<br /> {
<br /> int i;
<br /> i = 10;
<br /> printf("i : %d\n",i);
<br /> printf("sizeof(i++) is: %d\n",sizeof(i++));
<br /> printf("i : %d\n",i);
<br /> return 0;
<br /> }
<br />
<br />Why does the following program give a warning? (Please remember that sending a normal pointer to a function requiring const pointer does not give any warning)
<br />
<br /> #include <stdio.h>
<br /> void foo(const char **p) { }
<br /> int main(int argc, char **argv)
<br /> {
<br /> foo(argv);
<br /> return 0;
<br /> }
<br />
<br />What is the output of the following program?
<br />
<br /> #include <stdio.h>
<br /> int main()
<br /> {
<br /> int i;
<br /> i = 1,2,3;
<br /> printf("i:%d\n",i);
<br /> return 0;
<br /> }
<br />
<br />The following is a piece of code which implements the reverse Polish Calculator. There is a(are) serious(s) bug in the code. Find it(them) out!!! Assume that the function getop returns the appropriate return values for operands, opcodes, EOF etc..
<br />
<br /> #include <stdio.h>
<br /> #include <stdlib.h>
<br />
<br /> #define MAX 80
<br /> #define NUMBER '0'
<br />
<br /> int getop(char[]);
<br /> void push(double);
<br /> double pop(void);
<br /> int main()
<br /> {
<br /> int type;
<br /> char s[MAX];
<br />
<br /> while((type = getop(s)) != EOF)
<br /> {
<br /> switch(type)
<br /> {
<br /> case NUMBER:
<br /> push(atof(s));
<br /> break;
<br /> case '+':
<br /> push(pop() + pop());
<br /> break;
<br /> case '*':
<br /> push(pop() * pop());
<br /> break;
<br /> case '-':
<br /> push(pop() - pop());
<br /> break;
<br /> case '/':
<br /> push(pop() / pop());
<br /> break;
<br /> /* ...
<br /> * ...
<br /> * ...
<br /> */
<br /> }
<br /> }
<br /> }
<br />
<br />The following is a simple program which implements a minimal version of banner command available on most *nix systems. Find out the logic used in the program.
<br />
<br /> #include<stdio.h>
<br /> #include<ctype.h>
<br />
<br /> char t[]={
<br /> 0,0,0,0,0,0,12,18,33,63,
<br /> 33,33,62,32,62,33,33,62,30,33,
<br /> 32,32,33,30,62,33,33,33,33,62,
<br /> 63,32,62,32,32,63,63,32,62,32,
<br /> 32,32,30,33,32,39,33,30,33,33,
<br /> 63,33,33,33,4,4,4,4,4,4,
<br /> 1,1,1,1,33,30,33,34,60,36,
<br /> 34,33,32,32,32,32,32,63,33,51,
<br /> 45,33,33,33,33,49,41,37,35,33,
<br /> 30,33,33,33,33,30,62,33,33,62,
<br /> 32,32,30,33,33,37,34,29,62,33,
<br /> 33,62,34,33,30,32,30,1,33,30,
<br /> 31,4,4,4,4,4,33,33,33,33,
<br /> 33,30,33,33,33,33,18,12,33,33,
<br /> 33,45,51,33,33,18,12,12,18,33,
<br /> 17,10,4,4,4,4,63,2,4,8,
<br /> 16,63
<br /> };
<br />
<br /> int main(int argc,char** argv)
<br /> {
<br />
<br /> int r,pr;
<br /> for(r=0;r<6;++r)
<br /> {
<br /> char *p=argv[1];
<br />
<br /> while(pr&&*p)
<br /> {
<br /> int o=(toupper(*p++)-'A')*6+6+r;
<br /> o=(o<0||o>=sizeof(t))?0:o;
<br /> for(pr=5;pr>=-1;--pr)
<br /> {
<br /> printf("%c",( ( (pr>=0) && (t[o]&(1<<pr)))?'#':' '));
<br />
<br /> }
<br /> }
<br /> printf("\n");
<br /> }
<br /> return 0;
<br /> }
<br />
<br />What is the output of the following program?
<br />
<br /> #include <stdio.h>
<br /> #include <stdlib.h>
<br />
<br /> #define SIZEOF(arr) (sizeof(arr)/sizeof(arr[0]))
<br />
<br /> #define PrintInt(expr) printf("%s:%d\n",#expr,(expr))
<br /> int main()
<br /> {
<br /> /* The powers of 10 */
<br /> int pot[] = {
<br /> 0001,
<br /> 0010,
<br /> 0100,
<br /> 1000
<br /> };
<br /> int i;
<br />
<br /> for(i=0;i<SIZEOF(pot);i++)
<br /> PrintInt(pot[i]);
<br /> return 0;
<br /> }
<br />
<br />The following is the implementation of the Euclid's algorithm for finding the G.C.D(Greatest Common divisor) of two integers. Explain the logic for the below implementation and think of any possible improvements on the current implementation.
<br />BTW, what does scanf function return?
<br />
<br /> #include <stdio.h>
<br /> int gcd(int u,int v)
<br /> {
<br /> int t;
<br /> while(v > 0)
<br /> {
<br /> if(u > v)
<br /> {
<br /> t = u;
<br /> u = v;
<br /> v = t;
<br /> }
<br /> v = v-u;
<br /> }
<br /> return u;
<br /> }
<br />
<br /> int main()
<br /> {
<br /> int x,y;
<br /> printf("Enter x y to find their gcd:");
<br /> while(scanf("%d%d",&x, &y) != EOF)
<br /> {
<br /> if(x >0 && y>0)
<br /> printf("%d %d %d\n",x,y,gcd(x,y));
<br /> printf("Enter x y to find their gcd:");
<br /> }
<br /> printf("\n");
<br /> return 0;
<br /> }
<br />
<br />Also implement a C function similar to the above to find the GCD of 4 integers. What's the output of the following program. (No, it's not 10!!!)
<br />
<br /> #include <stdio.h>
<br /> #define PrintInt(expr) printf("%s : %d\n",#expr,(expr))
<br /> int main()
<br /> {
<br /> int y = 100;
<br /> int *p;
<br /> p = malloc(sizeof(int));
<br /> *p = 10;
<br /> y = y/*p; /*dividing y by *p */;
<br /> PrintInt(y);
<br /> return 0;
<br /> }
<br />
<br />The following is a simple C program to read a date and print the date. Run it and explain the behaviour
<br />
<br /> #include <stdio.h>
<br /> int main()
<br /> {
<br /> int day,month,year;
<br /> printf("Enter the date (dd-mm-yyyy) format including -'s:");
<br /> scanf("%d-%d-%d",&day,&month,&year);
<br /> printf("The date you have entered is %d-%d-%d\n",day,month,year);
<br /> return 0;
<br /> }
<br />
<br />The following is a simple C program to read and print an integer. But it is not working properly. What is(are) the mistake(s)?
<br />
<br /> #include <stdio.h>
<br /> int main()
<br /> {
<br /> int n;
<br /> printf("Enter a number:\n");
<br /> scanf("%d\n",n);
<br />
<br /> printf("You entered %d \n",n);
<br /> return 0;
<br /> }
<br />
<br />The following is a simple C program which tries to multiply an integer by 5 using the bitwise operations. But it doesn't do so. Explain the reason for the wrong behaviour of the program.
<br />
<br /> #include <stdio.h>
<br /> #define PrintInt(expr) printf("%s : %d\n",#expr,(expr))
<br /> int FiveTimes(int a)
<br /> {
<br /> int t;
<br /> t = a<<2 + a;
<br /> return t;
<br /> }
<br />
<br /> int main()
<br /> {
<br /> int a = 1, b = 2,c = 3;
<br /> PrintInt(FiveTimes(a));
<br /> PrintInt(FiveTimes(b));
<br /> PrintInt(FiveTimes(c));
<br /> return 0;
<br /> }
<br />
<br />Is the following a valid C program?
<br />
<br /> #include <stdio.h>
<br /> #define PrintInt(expr) printf("%s : %d\n",#expr,(expr))
<br /> int max(int x, int y)
<br /> {
<br /> (x > y) ? return x : return y;
<br /> }
<br />
<br /> int main()
<br /> {
<br /> int a = 10, b = 20;
<br /> PrintInt(a);
<br /> PrintInt(b);
<br /> PrintInt(max(a,b));
<br /> }
<br />
<br />The following is a piece of C code, whose intention was to print a minus sign 20 times. But you can notice that, it doesn't work.
<br />
<br /> #include <stdio.h>
<br /> int main()
<br /> {
<br /> int i;
<br /> int n = 20;
<br /> for( i = 0; i < n; i-- )
<br /> printf("-");
<br /> return 0;
<br /> }
<br />
<br />Well fixing the above code is straight-forward. To make the problem interesting, you have to fix the above code, by changing exactly one character. There are three known solutions. See if you can get all those three. What's the mistake in the following code?
<br />
<br /> #include <stdio.h>
<br /> int main()
<br /> {
<br /> int* ptr1,ptr2;
<br /> ptr1 = malloc(sizeof(int));
<br /> ptr2 = ptr1;
<br /> *ptr2 = 10;
<br /> return 0;
<br /> }
<br />
<br />What is the output of the following program?
<br />
<br /> #include <stdio.h>
<br /> int main()
<br /> {
<br /> int cnt = 5, a;
<br />
<br /> do {
<br /> a /= cnt;
<br /> } while (cnt --);
<br />
<br /> printf ("%d\n", a);
<br /> return 0;
<br /> }
<br />
<br />What is the output of the following program?
<br />
<br /> #include <stdio.h>
<br /> int main()
<br /> {
<br /> int i = 6;
<br /> if( ((++i < 7) && ( i++/6)) || (++i <= 9))
<br /> ;
<br /> printf("%d\n",i);
<br /> return 0;
<br /> }
<br />
<br />What is the bug in the following program?
<br />
<br /> #include <stdlib.h>
<br /> #include <stdio.h>
<br /> #define SIZE 15
<br /> int main()
<br /> {
<br /> int *a, i;
<br />
<br /> a = malloc(SIZE*sizeof(int));
<br />
<br /> for (i=0; i<SIZE; i++)
<br /> *(a + i) = i * i;
<br /> for (i=0; i<SIZE; i++)
<br /> printf("%d\n", *a++);
<br /> free(a);
<br /> return 0;
<br /> }
<br />
<br />Is the following a valid C program? If so, what is the output of it?
<br />
<br /> #include <stdio.h>
<br /> int main()
<br /> {
<br /> int a=3, b = 5;
<br />
<br /> printf(&a["Ya!Hello! how is this? %s\n"], &b["junk/super"]);
<br /> printf(&a["WHAT%c%c%c %c%c %c !\n"], 1["this"],
<br /> 2["beauty"],0["tool"],0["is"],3["sensitive"],4["CCCCCC"]);
<br /> return 0;
<br /> }
<br />
<br />What is the output of the following, if the input provided is:
<br />Life is beautiful
<br />
<br /> #include <stdio.h>
<br /> int main()
<br /> {
<br /> char dummy[80];
<br /> printf("Enter a string:\n");
<br /> scanf("%[^a]",dummy);
<br /> printf("%s\n",dummy);
<br /> return 0;
<br /> }
<br />
<br />Note : This question has more to do with Linker than C language
<br />We have three files a.c, b.c and main.c respectively as follows:
<br />a.c
<br />---
<br />
<br />int a;
<br />
<br />b.c
<br />---
<br />
<br />int a = 10;
<br />
<br />main.c
<br />------
<br />
<br />extern int a;
<br />int main()
<br />{
<br /> printf("a = %d\n",a);
<br /> return 0;
<br />}
<br />
<br />Let's see what happens, when the files are compiled together:
<br />
<br />bash$ gcc a.c b.c main.c
<br />bash$ ./a.out
<br />a = 10
<br />
<br />Hmm!! no compilation/linker error!!! Why is it so?? The following is the offset macros which is used many a times. Figure out what is it trying to do and what is the advantage of using it.
<br />
<br /> #define offsetof(a,b) ((int)(&(((a*)(0))->b)))
<br />
<br />The following is the macro implementation of the famous, Triple xor swap.
<br />
<br /> #define SWAP(a,b) ((a) ^= (b) ^= (a) ^= (b))
<br />
<br />What are the potential problems with the above macro? What is the use of the following macro?
<br />
<br /> #define DPRINTF(x) printf("%s:%d\n",#x,x)
<br />
<br />Let's say you were asked to code a function IAddOverFlow which takes three parameters, pointer to an integer where the result is to be stored, and the two integers which needs to be added. It returns 0 if there is an overflow and 1 otherwise:
<br />
<br /> int IAddOverFlow(int* result,int a,int b)
<br /> {
<br /> /* ... */
<br /> }
<br />
<br />So, how do you code the above function? (To put in a nutshell, what is the logic you use for overflow detection?) What does the following macro do?
<br />
<br /> #define ROUNDUP(x,n) ((x+n-1)&(~(n-1)))
<br />
<br />Most of the C programming books, give the following example for the definition of macros.
<br />
<br /> #define isupper(c) (((c) >= 'A') && ((c) <= 'Z'))
<br />
<br />But there would be a serious problem with the above definition of macro, if it is used as follows (what is the problem??)
<br />
<br /> char c;
<br /> /* ... */
<br /> if(isupper(c++))
<br /> {
<br /> /* ... */
<br /> }
<br />
<br />But most of the libraries implement the isupper (declared in ctypes.h) as a macro (without any side effects). Find out how isupper() is implemented on your system. I hope you know that ellipsis (...) is used to specify variable number of arguments to a function. (What is the function prototype declaration for printf?) What is wrong with the following delcaration?
<br />
<br /> int VarArguments(...)
<br /> {
<br /> /*....*/
<br /> return 0;
<br /> }
<br />
<br />Write a C program to find the smallest of three integers, without using any of the comparision operators. What does the format specifier %n of printf function do? Write a C function which does the addition of two integers without using the '+' operator. You can use only the bitwise operators.(Remember the good old method of implementing the full-adder circuit using the or, and, xor gates....) How do you print I can print % using the printf function? (Remember % is used as a format specifier!!!) What's the difference between the following two C statements?
<br />
<br /> const char *p;
<br /> char* const p;
<br />
<br />What is the difference between memcpy and memmove? What is the format specifiers for printf to print double and float values? Write a small C program to determine whether a machine's type is little-endian or big-endian. Write a C program which prints Hello World! without using a semicolon!!!</span>
<br /><strong></strong><strong></strong><strong></strong><strong></strong><div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4950233072027624659-2848539236707837554?l=technicalpuzzles.blogspot.com'/></div>techniehttp://www.blogger.com/profile/13608653955259756995noreply@blogger.com0tag:blogger.com,1999:blog-4950233072027624659.post-74985681298273644912008-05-19T14:44:00.000-07:002008-05-19T14:48:18.813-07:00C puzzles,<br /><br />C++ puzzles,<br /><br />Java Puzzles,<br /><br />Data Structure Puzzles,<br /><br />Os Puzzles<br /><br />.net Puzzles<div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4950233072027624659-7498568129827364491?l=technicalpuzzles.blogspot.com'/></div>techniehttp://www.blogger.com/profile/13608653955259756995noreply@blogger.com0