Tag: programming languages

Questions Related to programming languages

void main() { int i = 10; long *p=&i; p++; printf("%d",abs(&i-p)); }

  1. compile error

  2. 2

  3. 1

  4. 10


Correct Option: B

void main(){ char far *p = 0x417; *p=64; }

  1. Reboot the Machine

  2. Disable the print screen key

  3. Change the default screen color

  4. puts Capslock on


Correct Option: D

void main() { int a[2][3]={{1,2,3},{4,5,6}}; printf("%d\n",++**(a+1)); }

  1. 5

  2. garbage value

  3. compile error

  4. 2


Correct Option: A

ejbCreate() method of CMP bean returns

  1. null

  2. Primary Key class

  3. Home Object

  4. Remote Object


Correct Option: A

Which of the following statements hold true in the following program? package generics; import java.util.ArrayList; import java.util.List; public class Ques04 { public static void main(String[] args) { List extends Instrument> allInstruments = new ArrayList(); // -->X allInstruments.add(new Guitar()); allInstruments.add(new Violin()); } } interface Instrument { public void play(); } class Guitar implements Instrument { public void play(){ System.out.println("Playing Guitar."); } } class Violin implements Instrument { public void play(){ System.out.println("Playing Violin."); } }

  1. The program will not compile.

  2. The program will compile but will throw run-time exception.

  3. The statement 'X' manifests that objects of type Instrument as well as sub-types can be added to the list 'allInstruments'.

  4. It is not possible to add any type of objects to the list 'allInstruments'.


Correct Option: A

what is the output of compiling an running the following code? import java.util.*; class CheckTest { public static void main(String [] args) { Hashtable ht = new Hashtable(); ht.put("chec", "check"); ht.put(1000, "check"); // line 1 ht.put("check", 20.01); // line 2 System.out.print(ht.get("chec") + " "); System.out.print(ht.get(1000) + " "); //line 3 System.out.print(ht.get("check")); //line 4 } }

  1. Compilation fails due to error at line 1,2,3,4

  2. Compilation fails due to error at line 3,4

  3. Compiles fine and exception is thrown at runtime.

  4. Compiles fine and prints "check check 20.01"


Correct Option: D
Explanation:

To solve this question, the user needs to understand the concepts of Hashtable and data types in Java.

The code creates a Hashtable named ht, which maps keys to values. It puts three key-value pairs into the Hashtable:

  • "chec" is mapped to "check"
  • 1000 is mapped to "check"
  • "check" is mapped to 20.01

The code then retrieves the values corresponding to three keys and prints them out.

Now let's go through each option and explain why it is right or wrong:

Option A: Compilation fails due to error at line 1,2,3,4 This option is incorrect. There are no compilation errors in this code.

Option B: Compilation fails due to error at line 3,4 This option is incorrect. There are no compilation errors in this code.

Option C: Compiles fine and exception is thrown at runtime. This option is incorrect. The code compiles without errors, but no exception is thrown at runtime. The output of the code is "check check 20.01".

Option D: Compiles fine and prints "check check 20.01" This option is correct. The code compiles without errors, and when run, it prints "check check 20.01". The keys "chec" and 1000 are mapped to the value "check", while the key "check" is mapped to the value 20.01.

Therefore, the answer is: D. Compiles fine and prints "check check 20.01".

A JSP page is opened in a particular Session. A button is present in that JSP page onclick of which a new Window gets opened.

  1. The Session is not valid in the new Window

  2. The Session is valid in the new Window

  3. The Scope is not valid in the new Window

  4. The Scope is valid in the new Window


Correct Option: B

AI Explanation

To answer this question, we need to understand how sessions and scopes work in web applications.

When a user opens a JSP page, a session is created for that user. The session allows the server to store and retrieve information specific to that user throughout their browsing session.

In this scenario, when the user clicks on the button and a new window opens, the session is still valid in the new window. This means that the server can still access and retrieve the session data for that user.

On the other hand, scopes in JSP refer to the lifespan and visibility of variables within a JSP page. The most commonly used scopes are request, session, and application. These scopes determine how long a variable will exist and be accessible within a JSP page.

However, the scope of variables does not directly affect the validity of the session in the new window. The session remains valid regardless of the scope of variables used in the JSP page.

Therefore, the correct answer is B) The Session is valid in the new Window.

In the init(ServletConfig) method of Servlet life cycle, what method can be used to access the ServletConfig object ?

  1. getServletInfo()

  2. getInitParameters()

  3. getServletConfig()

  4. None of the above


Correct Option: C

What is the output for the following lines of code? import java.io.*; public class FileHandling { public static void main (String[] arg) throws Exception { File f = new File ("ll.txt"); FileOutputStream s = new FileOutputStream(f); PrintStream p = new PrintStream (s); p.println("hi"); } }

  1. A. It would fail to compile

  2. B. It would generate a run-time error on execution.

  3. C. It would execute, but ask the user to specify the directory in which to create ll.txt.

  4. D. It would execute without an error and print "hi" to a file called ll.txt


Correct Option: D