Monday 8 April 2013

printf("%p...%p...%p",p,q,r);


    main()
{
 char *p;
 int *q;
 long *r;
 p=q=r=0;
 p++;
 q++;
 r++;
 printf("%p...%p...%p",p,q,r);
}
Answer:
0001...0002...0004
Explanation:
++ operator  when applied to pointers increments address according to their corresponding data-types.

Blog Author: Vijay Kumar

char temp[ ] = “string";


  char *someFun1()
    {
    char temp[ ] = “string";
    return temp;
    }
    char *someFun2()
    {
    char temp[ ] = {‘s’, ‘t’,’r’,’i’,’n’,’g’};
    return temp;
    }
    int main()
    {
    puts(someFun1());
    puts(someFun2());
    }
Answer:
    Garbage values.
Explanation:
    Both the functions suffer from the problem of dangling pointers. In someFun1() temp is a character array and so the space for it is allocated in heap and is initialized with character string “string”. This is created dynamically as the function is called, so is also deleted dynamically on exiting the function so the string data is not available in the calling function main() leading to print some garbage values. The function someFun2() also suffers from the same problem but the problem can be easily identified in this case.


Blog Author: Vijay Kumar

char *temp = “string constant";


char *someFun()
    {
    char *temp = “string constant";
    return temp;
    }
    int main()
    {
    puts(someFun());
    }
Answer:
    string constant
Explanation:
    The program suffers no problem and gives the output correctly because the character constants are stored in code/data area and not allocated in stack, so this doesn’t lead to dangling pointers.


Blog Author: Vijay Kumar

void *k;


    main()
{
         int a=10,*j;
    void *k;
         j=k=&a;
    j++; 
         k++;
    printf("\n %u %u ",j,k);
}
Answer:
         Compiler error: Cannot increment a void pointer
Explanation:
Void pointers are generic pointers and they can be used only when the type is not known and as an intermediate address storage type. No pointer arithmetic can be done on it and you cannot apply indirection operator (*) on void pointers.

Blog Author: Vijay Kumar

char a[4]="HELL"; must check


    main()
{  
char a[4]="HELL";
printf("%s",a);
}
Answer:
         HELL%@!~@!@???@~~!
Explanation:
The character array has the memory just enough to hold the string “HELL” and doesnt have enough space to store the terminating null character. So it prints the HELL correctly and continues to print garbage values till it     accidentally comes across a NULL character.

Blog Author: Vijay Kumar

char a[4]="HELLO";


    main()
{
char a[4]="HELLO";
printf("%s",a);
}  
Answer:
         Compiler error: Too many initializers
Explanation:
The array a is of size 4 but the string constant requires 6 bytes to get stored.


Blog Author: Vijay Kumar

Is this code legal?


    Is this code legal?
int *ptr;
ptr = (int *) 0x400;
Answer:
         Yes
Explanation:
The pointer ptr will point at the integer in the memory location 0x400.


Blog Author: Vijay Kumar

Is the following code legal?


    Is the following code legal?
struct a
    {
int x;
            struct a *b;
    }
Answer:
Yes.
Explanation:
*b is a pointer to type struct a and so is legal. The compiler knows, the size of the pointer to a structure even before the size of the structure
is determined(as you know the pointer to any type is of same size). This type of structures is known as ‘self-referencing’ structure.

Blog Author: Vijay Kumar

JPC 1


public class Loop {
    public static void main(String[] args){
         int i=1;
         int j=5;

         for(;;){
             System.out.println(j>>>i);
         }
    }
}

What will be output of the above java program?

(a)2
   1
(b)2
   1
   0
(c)Infinite loop
(d)Compiler error

Answer: (c)

Because No Condition Check in For Loop.


Basics core java questions and answers

Basics core java questions and answers 
(1) Which of the following option is not true about java programming language? 

(a) Java is high level programming language.
(b) Java is a platform.
(c) javac is compiler.
(d) Byte code is executed by CPU. 

Answer: (d)

(2) Which is a not characteristic of java programming language? 

(a) Robust
(b)Procedural
(c) Distributed
(e) Multithreaded

Answer: (b)

(3) Which of the following is true about of java hotspot virtual machine? 

(a) It is additional virtual machine which improves the performance of an application.
(b) It is internal device which convert source code into byte code.
(c) It is virtual machine which detects runtime exception.
(d) All are true.

 Answer: (a)

(4) Which of the following is correct?

(a) Java platform is software only platform that runs on the top of other hardware based platform. 
(b) Java platform has two components: JVM, API.
(c) Java technology is programming language as well as platform
(d) All of the above.

Answer: (d) 

(5) Which of them is a not command line tool?

(a) java
(b) javaw
(c) javapath
(d) javadoc 

Answer: (c) 

(6) Which of the following is correct about main method in java?

(a) Must be declared as public. 
(b) It must not return any value.
(c) Must be declared as static.
(e) Must accept string as a parameter.
(f) Above all are compulsory. 
(g) All are optional.

 Answer: (f)

(7) Which not true about API in java?

(a) API stands for application package interface. 
(b) It is large collection of software components.
(c) It is large array of useful class.
(d) It is grouped into the package of related class. 

Answer: (a) 

(8) Which of the following is correct? 

(a)

class MainClass {
     static public void main(String[] args) {
          System.out.println("Hello World!");
     }
}

(b)

class MainClass {
     public static void main(String[] args) {
          System.out.println("Hello World!");
     }
}

(c)

 class MainClass {
      static public int main(String[] args) {
           System.out.println("Hello World!");
           return 1;
     }
}

(d) Both option (a) and (b)

 Answer: (d) 

(9) Which of the following is not true in java? 

(a) Java platform is bit faster than actual CPU platform.
(b) javac is compiler tool. 
(c) java command launch an application. 
(d) javadoc is documentation tool. 

Answer: (a) 

(10) What is difference between following two commands?

(i) java ClassName
(ii)javaw ClassName

Where ClassName.class is name of any class file. 

(a) Execution time of (i) is faster than (ii) 
(b) (i) is used to run in server machine while (ii) is used to run in client machine.
(c) (ii) is used to run in server machine while (i) is used to run in client machine. 
(d) (i) has console window while (ii) has not any console window. 

Answer: (d) 

(11) If any class file Binary.class is zipped in Binary.zip at c:\Test. Which of the following command line command is suitable to execute the zip file?

(a) java –classpath c:\Test\Binary.zip Binary
(b) java –cp c:\Test\Binary.zip Binary
(c) javaw –classpath c:\Test\Binary.zip Binary 
(d) javaw –cp c:\Test\Binary.zip Binary
(e) All of them are correct.

Answer: (e) 

(12) How can we set the following two path of class folder at a time in command prompt?

(i) c:\test
(ii) Current working directory.

(a) set classpath=c:\test;.
(b) set -cp=c:\test;.
(c) path=c:\test;. 
(d) PATH=. ; c:\test 

Answer: (a) 

(13) If Binary.class is present is at both of following directory

(i) c:\test
(ii) d:\raja

Which of them is true if following command is executed in the command prompt?

set cp=c:\test;d:\raja java Binary

(a) Binary.class, which is present in test folder will execute only.
(b) Binary.class, which is present in raja folder will execute only. 
(c) Binary.class, which are present in test and raja folders both will execute. 
(d) It will depend upon O.S.

 Answer: (a)



Blog Author: Vijay Kumar