Summary Class notes - Introductory Programming

Course
- Introductory Programming
- n/a
- 2017 - 2018
- UCL
- CS
360 Flashcards & Notes
1 Students
  • This summary

  • +380.000 other summaries

  • A unique study tool

  • A rehearsal system for this summary

  • Studycoaching with videos

Remember faster, study better. Scientifically proven.

PREMIUM summaries are quality controlled, selected summaries prepared for you to help you achieve your study goals faster!

Summary - Class notes - Introductory Programming

  • 1506895200 Week 0

  • What is a variable?
    A piece of your computer's memory that is given a name and type, and can store a value




  • What is the declaration statement syntax of a variable?




    <type> <name> ;




  • Is it legal to declare multiple variables of the same type on one line? If so, how?
    Yes - 



    , , ..., ; 
  • Declaring a variable sets aside a chunk of memory in which you can store a value
  • What is an assignment statement?
    A Java statement that stores a value into a variable's memory location
  • Which assignment operator has the lowest precedence (i.e. which is carried out last): '+', '='. '*'?
    =
  • Why will int x = 2.5; raise an error?
    Because x is declared an int variable = it can only store an int value (not a float)
  • Can an int value be stored in a variable of a different data type?
    Yes - an int value can be stored in a variable of type double; a float variable can also accept an int
  • How many primitive datatypes does Java have? What are they?
    8

    Numeric:
    - byte (integer)
    - short (integer)
    - long (integer)
    - int (integer)
    - double (floating point)
    - float (floating point)  

    Non-numeric
    - character
    - boolean
  • Java has primitive data types and non primitive data types. What are the non primitive data types called? How many are there?
    Types that are not primitive are called object/reference types 

    3
    - strings
    - arrays
    - user defines classes
  • How many bits does a boolean data type contain?
    1 bit
  • How many bits does a char data type contain?
    16 bits
  • How many bits does a byte data type contain?
    8 bits
  • How many bits does a short data type contain?
    16 bits
  • How many bits does a int data type contain?
    32 bits
  • How many bits does a long data type contain?
    64 bits
  • How many bits does a float data type contain?
    32 bits
  • How many bits does a double data type contain?
    64 bits
  • What are the benefits of constraining the size of a variable?
    Optimising memory
  • What is the default value of a boolean data type?
    False
  • What is the syntax to declare/initialise multiple variables at once?




    <type> <name> = <value> , <name> = <value> ;

    -> note, you only need to declare the type once
  • Which package do we import the Scanner from?




    import java.util.*;
  • What are the 4 Scanner methods and what do they do?


    • nextInt() -> reads a token of user input as an int
    • nextDouble() -> reads a token of user input as a double
    • next() -> reads a token of user input as a String
    • nextLine() -> reads a line of user input as a String
  • What is a token? What are tokens separated by?




    • token: A unit of user input, as read by the Scanner.
      – Tokens are separated by whitespace (spaces, tabs, newlines). 
  • Who are boolean values named after?




    George Boole (1815-1864) 
    invented logic and defined Boolean algebra
  • What relational operators do boolean expressions use?
    • ==
    • !=
    • <
    • <=
    • >
    • >=
  • What logical operators do boolean expressions use?
    • &&
    • ||
    • !


    --> remember to use parenthesis when using logical operators
  • Regarding relational operators and logical operators, which have the lowest precedence?




    Binary logical operators have lower precedence than relational operators (they will be evaluated after) 




  • In a decision structure’s simplest form certain statements are executed only when...
    ...a specific condition exists.




  • It is said that the statements inside of the 
    decision structure are... 
    ...conditionally executed.
  • What are conditional operators? What is the syntax of the conditional operator?








    The Conditional Operator is short expressions that work like if-else statements.

    Syntax: 
    BooleanExpression ? Value1 : Value2;

    – If Boolean Expression is true, Value 1 is returned
    – If Boolean Expression is false, Value 2 is returned 

    (slide 46)
  • Regarding switch statements, what is the switch expression?




    A variable or expression that has to be either char, byte, short, int or String 
  • The switch statement compares the value of SwitchExpression to each CaseExpressions
  • What is a loop?




    A control structure that causes a statement or group of statements to repeat. 
  • What is the general form of a while loop?




    while (BooleanExpression)
            Statement or Block 
  • When does a while loop stop executing?




    Until the test of the BooleanExpression results in false
  • What is the while loop known as? Why?
    A pretest loop
    -> because it tests the boolean expression before it executes the statements in its body
  • What is a loop control variable?




    A loop control variable determines how many times a loop
    repeats. 
  • What is the general form of a do-while loop?




    do
           Statement or Block
    while (BooleanExpression
    ); 






    • Here, the Statement or Block is executed first
    • Next, the BooleanExpression is tested
    – If true, the Statement or Block is executed 
    • Then the Boolean Expression is tested
    – This continues until the BooleanExpression is false
  • What is the do-while loop known as? Why?
    A postest loop-> because the boolean expression is tested at the end 





    Note that this means the Statement or Block will ALWAYS be executed at least once  (this contrasts with the while loop where if the condition is false, nothing will print)
  • What type of loops are the while and do-while loops?
    They are conditionally-controlled loops-> executes as long as a particular condition exists
  • What type of loop is a for loop?
    A count-controlled loop-> a loop that repeats a specific number of time
  • What is the general form of a for loop?




    for(Initialisation; Test; Update) 
          Statement or Block;  
  • Is the for loop a postest loop or a pretest loop?
    A pretest loop
  • What are postfix increment and decrement operators?
    Operators that are placed after the variable

    e.g. 




    • x++
    • x--   
  • What are prefix increment and decrement operators?
    Operators are placed before the variables

    e.g.




    • ++x
    • --x 
  • What's the difference between prefix and postfix increment and decrement operators?




    When the increment or decrement takes place.
    e.g.
    int x = 1, y;
    y = x++; 

          ---> y is1, x is 2.
    • The increment operator happened after the assignment operator.

    int x = 1, y;
    y = ++x;
          ---> y is 2, x is 2.
    • The increment operator happened before the assignment operator.
  • What two keywords can be used to modify the normal iteration of a loop?
    • break - the loop stops and the program jumps to the statement immediately after the loop block
    • continue - the current iteration is skipped
  • What 2 methods should you use to compare String variables?




    equals --> to check if two strings are the same
    e.g. StringReference.equals(OtherString); 

    compareTo 
  • What method would you use to make a case-insensitive comparison between Strings?




    equalsIgnoreCase() 
Read the full summary
This summary. +380.000 other summaries. A unique study tool. A rehearsal system for this summary. Studycoaching with videos.

Latest added flashcards

Which of these class is used to create user defined exception?a) java.langb) Exceptionc) RunTimed) System
b) Exception
Which of these exceptions will be thrown if we use null reference for an arithmetic operation?a) ArithmeticExceptionb) NullPointerExceptionc) IllegalAccessExceptiond) IllegalOperationException
b) NullPointerException

--> If we use null reference anywhere in the code where the value stored in that reference is used then NullPointerException occurs.
Which of these packages contain all the Java’s built in exceptions?a) java.iob) java.utilc) java.langd) java.net
c) java.lang
Which of these exceptions handles the situations when illegal argument is used to invoke a method?a) IllegalExceptionb) Argument Exceptionc) IllegalArgumentExceptiond) IllegalMethodArgumentExcepetion
c) IllegalArgumentException
Which of these packages contains abstract keyword?a) java.langb) java.utilc) java.iod) java.system
a) java.lang
Which of these methods is used to obtain value of invoking object as a long?a) long value()b) long longValue()c) Long longvalue()d) Long Longvalue()
b) long longValue()

---> long longValue() is used to obtain value of invoking object as a long
Which of the following is method of wrapper Integer for converting the value of an object into int?a) bytevalue()b) int intValue();c) Bytevalue()d) Byte Bytevalue()
b) int intValue();
Which of these is a super class of wrappers Double and Float?a) Longb) Digitsc) Floatd) Number
d) Number

---> Number is an abstract class containing subclasses Double, Float, Byte, Short, Integer and Long.
Which of these are selection statements in Java?a) if()b) for()c) continued) break
a) if()
---> Continue and break are jump statements, and for is an looping statement.
 Which of these selection statements test only for equality?a) ifb) switchc) if & switchd) none of the mentioned
b) switch

---> Switch statements checks for equality between the controlling variable and its constant cases.