Xii Important Questions With Solution Informatics Practices

download Xii Important Questions With Solution Informatics Practices

of 12

Transcript of Xii Important Questions With Solution Informatics Practices

  • 8/11/2019 Xii Important Questions With Solution Informatics Practices

    1/12

    XII Informatics Practices 2013 important questions with solution

    1. Why do you use import statement in java programming? Explain with example.

    Ans. The import statement in Java allows programmers to refer to classes which are declared in other

    packages to be accessed without referring to the full package name. Import statement must be the first

    line of the program. Import statement have 2 option for importing the classes from packages

    a. single class from package by mentioning the class name specifically. Eg. import

    javax.swing.jOptionPane;

    b. entire class by using * (asterisk) symbol in place of class name. Eg. import javax.swing.*;

    Classes from the java.lang package are automatically imported, so it is not require to explicitly

    importing.

    2. What will be the initial value of an object reference which is defined as an instance variable?

    Ans. Each instance variable is initialized with a default value when it is created:Type Initial / Default value Type Initial / Default value

    byte 0(Zero of byte type) double 0.0D

    short 0(Zero of byte type) char null character i.e., \u000

    int 0 boolean false

    long 0L All reference types null

    float 0.0F

    3. What is the purpose of if statement? Describe the different forms of if statements with example.

    Ans. An if-statement tests a particular condition; if the condition evaluates to true, a statement or set of

    statements is executed otherwise the statement or set of statements is not executed.

    There are four different forms of if statements which are as following:1. Simple if statement: Example:

    if(ch== )

    label1.setText(It is space character);

    2. If-else statement: Example:

    if(num>0)

    label1.setText(number is positive);

    else

    label1.setText(number is negative or zero);

    3. Nested if statement:

    if(num>0)

    {

    if(num

  • 8/11/2019 Xii Important Questions With Solution Informatics Practices

    2/12

    method is called a local variable.

    2. Global variable: If a variable is declare outside all methods, then its scope includes all the

    methods and it is said to be a global variable.

    5. What do you understand about inheritance? Write the advantage of inheritance.

    Ans. Inheritance is a form of software reusability in which new classes are created from existing classes by

    absorbing their attributes and behaviors.

    Advantages:

    Inheritance is capable of expressing the inheritance relationship of real world models. Men

    inherit from person; woman inherits from person etc.

    Inheritance facilitates the code reusability. Additional features can be added to a class by

    deriving a class from it and then by adding new features to it. Class once written and tested

    need not be re-written or re-defined.

    Inheritance is capable of simulating the transitive nature of real worlds inheritance, which in turn saves

    on modification time and efforts, if required.

    6. Difference between if-else and switch-case.

    Ans. if-else switch-caseIf can evaluate a relational or logical expression

    i.e., multiple conditions.

    Switch can only test for equality.

    The if-else constructions lets you use a series of

    expressions that may involve unrelated variables

    and complex expression.

    The switch statement selects its branches by

    testing the value of same variable.

    The if-else statement can handle floating point

    test also a part from handling integer and

    character tests.

    A switch cannot handle floating point tests. The

    case labels of switch must be an integer byte,

    short, int or a char.

    7. Difference between while and do-while.

    Ans. while do-whileWhile is an entry-controlled loop. do-while is a exit-controlled loop.

    In while loop the test expression is evaluated at

    the end of the loop i.e., after executing the loop

    body.

    In do-while loop the test expression is evaluated

    at the beginning of the loop i.e., before executing

    the loop body.

    Syntax:

    while(test-expression)

    {

    // statement

    }

    Syntax:

    do

    {

    // statement

    } while(test-expression);

    8. What are the steps required to execute a query in JBDC?Ans. 1. Import the packages required for database programming.

    2. Register the JDBC Driver.

    3. Open a connection.

    4. Execute a query.

    5. Extract data from result set.

    6. Clean up the environment.

    9. List the Advantages of JDBC.

    Ans. Provide Existing Enterprise Data

    CBSE CS N IP Page 2 of

    http://www.cbsecsnip.in/http://www.cbsecsnip.in/
  • 8/11/2019 Xii Important Questions With Solution Informatics Practices

    3/12

    Simplified Enterprise Development

    Zero Configuration for Network Computers

    Full Access to Metadata

    No Installation

    Database Connection Identified by URL

    10. Difference between JTextField and j PasswordField.

    Ans. When we type text into a JTextField control, it shows the characters in the control, but in

    JPasswordField control the typed characters are shown as astriks for security.

    11. Differentiate between Java and NetBeans. What is IDE?

    Ans. Java is both a programming language and a platform whereas NetBeans is a free, open source,

    cross-paltform IDE with built in support for java programming language.

    IDE: Integrated Devlopment Envirnment(IDE), its a software tool to help programmer to edit

    source code, compile, interpreate and debug.

    12. What is ByteCode. Explain JVM.

    Ans. ByteCode: A bytecode is a byte-long instruction that the Java compiler generates and the Java

    interpreater executes.JVM: JVM(Java Virtual Machine) is a program which behaves as interpreter and translate bytecode into

    machine languages as they go- called just-in-time compilation(JIT).

    13. Differentiate between ODBC and JDBC driver.

    Ans.

    ODBC JDBC

    odbc is open database connectivity. jdbc is java database connectivity.

    OBDC is for Microsoft. JDBC is for Java applications.

    ODBC mixes simple and advanced

    features together and has complex

    options for simple queries.

    JDBC is designed to keep things simple

    while allowing advanced capabilities

    when required. ODBC requires manual installation of the

    ODBC driver manager and driver on all

    client machines.

    JDBC drivers are written in Java and JDBC

    code is automatically installable, secure,

    and portable on all platforms.

    14. Differentiate between Call by Value and Call by Reference.

    Ans. Call By Value Call by reference

    Call by value is used to create a temporary copy

    of the data which is transferred from the actual

    parameter in the final parameter.

    Call by reference is used to share the same

    memory location for actual and formal

    parameters

    The changes done in the function in formal

    parameter are not reflected back in the callingenvironment.

    The changes done in the function are reflected

    back in the calling environment.

    It does not use & sign It makes the use of the & sign as the reference

    operator.

    15. What are containers or container controls?

    Ans. A containers are a controls that can hold other controls within it e.g., a Frame(there can be multiple

    controls inside a frame) or a label(it can hold an image and/or text) or simply window (we can put so

    many controls on it). Controls inside a container are known as child controls. The child controls can

    exist completely inside their containers. That means we cans move them outside their container.

    CBSE CS N IP Page 3 of

    http://www.cbsecsnip.in/http://www.cbsecsnip.in/
  • 8/11/2019 Xii Important Questions With Solution Informatics Practices

    4/12

    When we delete a container control, all its child controls automatically get deleted.

    16. What is a list? How is it different from combo box?

    Ans. A List is a graphical control that displays a list of items in a box wherefrom the user can make

    selections

    Difference between List and ComboBox

    List ComboBox

    A list doesnt have a text field the user can

    used to edit the selected item.

    A combobox is a cross between a text field

    and a list.

    In a list, the user must select items directly

    from the list.

    In a combobox user can edit it if he/she

    whishes.

    The list doesnt drop down. A combobox takes less space initially but

    drops down when user clicks on its arrow.

    Lists allow us to select more than one item. Combobox allows only single item

    selection.

    17. What do you mean by life time of variable? How is a local variable different from other variable?

    Ans. The lifetime of variable is the period during which it can be accessed.

    A local variable is declared inside a method and is accessible only inside that method. In this

    way local variable is different from other variable.

    18. What is the purpose of default clause in a switch statement?

    Ans. Default is used for the else situation. The default statement gets executed when no match is found.

    19. While working in NetBeans, Raj included a Listbox in the form. Now she wants the list of her friend's

    names to be displayed in it. Which property of Listbox control should she use to do this?

    Ans. Modal property

    20. What is constructor? How can you call access super class constructor from sub class.

    Ans. Constructor: A member method with the same name as its class is called constructor and it is used to

    initialize the objects of that class type with a legal initial value.By using super keyword we can call access super class constructor from sub class.

    21. Ms. Samhita has developed a Java application through which the students of her school can view

    their marks by entering their admission number. The marks are displayed in various text fields. What

    should she do so that the students are able to view but not change their marks in text fields?

    Ans. By uncheck editable property of JTextField.

    22. What is the purpose of break statement in a loop?

    Ans. The break statement is used to break from an enclosing do, while, for, or switch statement. Break

    breaks the loop without executing the rest of the statements in the block.

    23. What is the use of super keyword?

    Ans. The first use of keyword super is to access the hidden data variables of the super class hidden bythe subclass.

    The second use of the keyword super in java is to call super class constructor in the subclass.

    24. What are the 3 types of error found in program? Explain them briefly.

    Ans. 1. Compile-Time Errors

    Compile-time errors (syntax errors and semantics errors) refer to the errors that violate the

    grammatical rules and regulations of a programming language.

    2. Run-Time Errors

    Run-time errors occur during the execution of a program.

    CBSE CS N IP Page 4 of

    http://www.cbsecsnip.in/http://www.cbsecsnip.in/
  • 8/11/2019 Xii Important Questions With Solution Informatics Practices

    5/12

    3. Logical Errors

    Logical errors occur due to mistaken analysis of the problem.

    25. What should be done so that only one of the radio buttons (Male and Female) can be selected at a

    time?

    Ans. We should use ButtonGroup component so that only one of the radio buttons (Male and Female) can

    be selected at a time.

    26. A phone number, consisting of 10 digits, is stored in a string variable strPhone. Now it is required to

    store this phone number in a Long type variable lngPhone. Write a Java statement to do this.

    Ans. String strPhone = "123456789";

    long lngPhone = Long.parseLong(strPhone);

    System.out.println(lngPhone);

    27. Write a java method to fetch the data from the employee table and display it in jTable the method

    already having Connection class object name con, Statement class Object name smt and ResultSet

    class object rs with the following query

    SELECT EMPNO, ENAME, JOB, SAL FROM EMP.

    Ans. import java.sql.*;import javax.swing.table.*;

    import javax.swing.JOptionPane.*;

    // button

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

    // TODO add your handling code here:

    DefaultTableModel model=(DefaultTableModel)jTable1.getModel();

    int rows=model.getRowCount();

    if(rows>0)

    {

    for(int i=0;i

  • 8/11/2019 Xii Important Questions With Solution Informatics Practices

    6/12

    System.out.println(no+"|"+name+"|"+job1+"|"+salary);

    model.addRow(new Object[]{no,name,job1,salary});

    }

    }catch(Exception e)

    { }

    }

    28. What is abstract method? Write a Java code to demonstrate abstract method.

    Ans. An abstract method is a method that is declared without an implementation (without braces, and

    followed by a semicolon). If a class includes abstract methods, the class itself must be declared abstract

    Example:

    Public abstract class Shape

    {

    String name;

    double area;

    public abstract void display();}

    Class Circle extend shape

    {

    double radius;

    double calcArea()

    {

    return 3.154 * radius * radius;

    }

    public void display()

    {

    System.out.println(radius= + radius);

    System.out.println(area= + calcArea());

    }

    }

    29. What is Package? Explain various built-in packages of Java.

    Ans. Package: A package is a Java language element used to group related classes under a common name.

    java.lang: Provides classes that are fundamental to the design of the Java programming

    language. The most important classes are Object, which is the root of the class hierarchy, and

    Class, instances of which represent classes at run time.

    java.util: Contains the collections framework, legacy collection classes, event model, date and

    time facilities, internationalization, and miscellaneous utility classes (a string tokenizer, arandom-number generator, and a bit array).

    30. What is Interface? Write syntax of Interface and implementation of Interface.

    Ans. Interface: Interface is similar to a class which may contain methods signature only but not bodies and it

    is a formal set of method and constant declarations that must be defined by the class that implements

    it.

    Syntax of Interface:

    interface interface_name {

    Interface_body

    CBSE CS N IP Page 6 of

    http://www.cbsecsnip.in/http://www.cbsecsnip.in/
  • 8/11/2019 Xii Important Questions With Solution Informatics Practices

    7/12

    }

    Syntax of implementation of Interface:

    class class_name implements interface_1, interface_2,. {

    Interface_body

    }

    31. Which method converts a string to an integer?

    Ans. parseInt()method converts a string to an integer.

    32. Write a method in Java that takes two integer arguments and returns power of it. Suppose x and y

    passing as an argument then it returns x to power y after calculation.

    Ans. int cal_power(int x, int y)

    {

    int x=Math.pow(x,y);

    return(x);

    }

    33. Write a Method in Java to take a number as argument and print the product of its digit, as if a

    number entered is 234 then the program gives o/p as 24.Ans. import java.io.*;

    public class JavaApplication6 {

    public static void main(String[] args)

    {

    int n, r = 0;

    int a=1;

    try{

    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

    System.out.println("Enter the number");

    n=Integer.parseInt(br.readLine());while( n != 0 )

    {

    r=n%10;

    a=a*(r);

    n=n/10;

    }

    System.out.println(" No is=\n "+a);

    }catch(Exception e)

    {}

    }

    }

    34. Write the Java Code for Finding Prime No.

    Ans. class prime

    {

    public static void main(String p[])

    {

    CBSE CS N IP Page 7 of

    http://www.cbsecsnip.in/http://www.cbsecsnip.in/
  • 8/11/2019 Xii Important Questions With Solution Informatics Practices

    8/12

    int i,j,s=0,d=0;

    for(i=1;i

  • 8/11/2019 Xii Important Questions With Solution Informatics Practices

    9/12

    public class JavaApplication14

    {

    public static void main(String[] args)

    {

    int n, reverse = 0;

    try{

    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

    System.out.println("Enter the number");

    n=Integer.parseInt(br.readLine());

    while( n != 0 )

    {

    reverse = reverse * 10;

    reverse = reverse + n%10;

    n = n/10;

    }

    System.out.println("Reverce No is=\n "+reverse);}catch(Exception e)

    {}

    }

    }

    37. Write the Java Code for Fibonacci Series.

    Ans. import java.io.*;

    class Fibonacci{

    public static void main(String args[])

    {

    int n=10,first=0,second=1,next,c;

    try{

    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

    System.out.println("Enter the number of terms ");

    n=Integer.parseInt(br.readLine());

    System.out.println("First terms of fibonacci series are :-\n"+n);

    for(c=0;c

  • 8/11/2019 Xii Important Questions With Solution Informatics Practices

    10/12

    }

    38. Write Factorial () function and pass an integer number as a argument and return a factorial of passed

    number by using recursion method.

    Ans. import java.io.*;

    class Factorial

    {

    long fact(long a)

    {

    if(a

  • 8/11/2019 Xii Important Questions With Solution Informatics Practices

    11/12

    g. getInstance()

    h. setSize()

    i. getSelectedValue()

    j. executeUpdate()

    k. getPassword()

    l. getSelectedItem()

    m. addItem()

    n. append()

    o. setVisible()

    p. createStatement()

    q. setEnabled()

    r. setEitable()

    Ans. a. getString() : getString() method is provided by ResultSet object and used for obtaining column

    data for a row.

    Example: String st=rs.getString(ABC);

    b. substring(): this method is used to return a part or substring of the String used to invoke themethod. The first argument represents the starting location of the substring.

    Example: String s=abcdefghi;

    System.out.println(s.substring(5));

    System.out.println(s.substring(5,8));

    c. length(): This method count and return the number of characters contained in the string object.

    Example: String str=Informatics Practices;

    System.out.println(str.length());

    d. capacity(): Returns the current capacity. The capacity is the amount of storage available for

    newly inserted characters, beyond which an allocation will occur.

    Example: String str=Informatics Practices;

    System.out.println(str. capacity());

    e. round() : Rounds to the nearest integer. So, if the value is more than half way towards the

    higher integer, the value is rounded up to the next integer.

    Example: Math.round(1.01);

    f. isSelected(): This is a JCheckBox and JRadioButton method which return the state of the button.

    True if the toggle button is selected, false if its not.

    Example: if(JRadioButton1.isSelected()==true)

    {

    JLable1.setText(selected);

    }

    else{

    JLable1.setText( not selected);

    }

    g. getInstance(): This method is used to get the instance of a object.

    Example: class abc

    {

    abc ab;

    ab.getInstance();

    CBSE CS N IP Page 11 of

    http://www.cbsecsnip.in/http://www.cbsecsnip.in/
  • 8/11/2019 Xii Important Questions With Solution Informatics Practices

    12/12

    }

    h. setSize(): setSize() is used for setting a size for JFrame.

    Example: Jframe1.setSize(100,100);

    i. getSelectedValue(): Returns the selected value when only a single item is selected in the list.

    When multiple items are selected, it returns the first selected value. Returns null if there is no

    selection.

    Example: String dur=(String)jList1.getSelectedValue();

    jLabel1.setText(dur);

    j. executeUpdate(): Execute the given SQL statement, which may be an INSERT, UPDATE or

    DELETE statement or SQL statement that returns nothing, such as SQL DDL statement.

    Example: String sql=DELETE FROM Employees;

    ResultSet rs=stmt. executeUpdate(sql);

    k. getPassword():getPassword()method is used to obtain password from a password field.

    Example: String ped=new String(pwdfld.getPassword());

    l. getSelectedItem(): Returns the selected item.

    Example: String dur=(String)jComboBox1.getSelectedItem();jLabel1.setText(dur);

    m. addItem(): Adds an item to the item list, in the end, of the combo box.

    Example: citycb.addItem(ABC);

    n. append(): Adds the specified text to the end of the text area.

    Example: jTextArea1.append("ABC");

    o. setVisible(): Makes the check box visible if true is passed otherwise hides the check box.

    Example:jchackBox1.setVisible(true);

    p. createStatement(): Creates a SQL statement object for building and submitting an SQL

    statement to the database.

    Example: Statement stmt = con.createStatement( );

    q. setEnabled(): Enables the check box if true is passed otherwise disable the check box.

    Example:jchackBox1.setEnabled(true);

    r. setEditable(): Sets whether the user can edit the text in the Text Field. Default is true.

    Example: NameTF.setEditable(True);

    CBSE CS N IP P 12 f

    http://www.cbsecsnip.in/http://www.cbsecsnip.in/