Basics
Module structure, variables and types, operators, conditions, loops, procedures and functions of the built-in language.
21 questions
JuniorTheoryVery commonWhat does the Экспорт keyword do for a procedure or a module variable?
What does the Экспорт keyword do for a procedure or a module variable?
Экспорт makes a method or a module variable visible outside its module: a common module's method is called as ИмяМодуля.Метод(), an object module's export method or variable through the object. Without it, the name stays inside its module.
Common mistakes
- ✗Thinking every method of a common module is visible to other modules: without
Экспортit stays private to its module - ✗Confusing
Экспортwith the common module's server-call flag:Экспортonly opens access and does not move the call to the server - ✗Declaring a variable in a common module: a common module holds only procedures and functions
Follow-up questions
- →Why can't a common module declare an export variable?
- →How do you call an export method of an object module from another module?
JuniorTheoryVery commonHow does a procedure differ from a function in the 1C built-in language?
How does a procedure differ from a function in the 1C built-in language?
A function returns a value with Возврат Значение and can stand inside an expression: Итого = СуммаЗаказа(Заказ). A procedure only does work and returns nothing, so it is called as a statement; Возврат in it just ends the procedure.
Common mistakes
- ✗Thinking a function cannot be called as a separate statement: its result may simply be ignored
- ✗Putting a procedure call inside an expression: a procedure returns no value, and such a call is an error
- ✗Expecting a function without
Возврат Значениеto fail: it returnsНеопределено
Follow-up questions
- →What does a function return if execution reaches
КонецФункцииwithoutВозврат? - →How do the 1C development standards suggest naming a procedure and a function?
MiddleTheoryVery commonHow are parameters passed by default, and what does Знач change for an array?
How are parameters passed by default, and what does Знач change for an array?
By default a parameter is passed by reference, so assigning to it changes the caller's variable. Знач copies the value, and an array's value is a reference: Добавить in the method still changes the caller's array; a new assignment stays local.
Common mistakes
- ✗Expecting
Значto protect an array's contents: it copies only the reference, and changes to the elements reach the caller - ✗Believing parameters are copies by default, as in many languages: without
Значthe method can replace the caller's value - ✗Treating a
Значparameter as read-only: the copy can be assigned freely
Follow-up questions
- →How would you protect an array passed to a method from being changed?
- →What happens when a literal such as
5is passed to a parameter withoutЗнач?
JuniorTheoryCommonWhat does the program print (lines separated by spaces)?
What does the program print (lines separated by spaces)?
А Нет
Open full question →Common mistakes
- ✗Expecting
Иto evaluate both sides: afterЛожьon the left the result is known and the right side is never called - ✗Expecting a function called inside an expression to print nothing: its
Сообщитьruns when it is called
Follow-up questions
- →Which side of
ИЛИis skipped when the left side isИстина? - →How does short-circuit evaluation guard a division by zero?
JuniorTheoryCommonWhat does the program print?
What does the program print?
10
Open full question →Common mistakes
- ✗Expecting
Аto stay 5, as in languages where a parameter is always a copy: withoutЗначthe parameter is passed by reference - ✗Expecting
Значto change nothing: withЗначthe method gets a copy, and the change never reachesА
Follow-up questions
- →What changes if
Удвоить(5)is called with a number instead of a variable? - →When is it worth marking a parameter with
Знач?
JuniorTheoryCommonWhat does the program print?
What does the program print?
135
Open full question →Common mistakes
- ✗Expecting
Прерватьto leave only theЕсли: it ends the nearest loop as a whole - ✗Mixing up
ПродолжитьandПрервать:Продолжитьjumps to the next pass, whileПрерватьleaves the loop
Follow-up questions
- →In nested loops, which loop does
Прерватьleave? - →Does the
Дляcounter still grow whenПродолжитьskips the rest of a pass?
JuniorTheoryCommonWhat does the program print?
What does the program print?
скидка 5%
Open full question →Common mistakes
- ✗Expecting the narrower condition
Сумма > 5000to win: branches are checked in order and the first true one runs - ✗Expecting every true branch to run: after the first true branch the rest are skipped
Follow-up questions
- →How should these branches be reordered to give 10% from 5000?
- →When does the
Иначеbranch run in this construct?
JuniorTheoryCommonWhat does the program print (lines separated by spaces)?
What does the program print (lines separated by spaces)?
10 5
Open full question →Common mistakes
- ✗Expecting
Счto be 4 after the loop: the counter grows after every pass and leaves the loop at 5 - ✗Expecting the loop counter to exist only inside the loop:
Счis an ordinary variable and is still there after the loop
Follow-up questions
- →What changes if the loop body changes the variable that holds the end value?
- →When does a
Покаloop fit better thanДля?
JuniorTheoryCommonWhat does the program print (lines separated by spaces)?
What does the program print (lines separated by spaces)?
50 Да
Open full question →Common mistakes
- ✗Expecting a function without
Возвратto return0: when it reachesКонецФункцииit returnsНеопределено - ✗Expecting the compiler to demand
Возвратon every branch: the module compiles, and the missing branch givesНеопределено
Follow-up questions
- →How would you rewrite
Скидкаso that every branch returns a number? - →What happens if a procedure is used inside an expression?
JuniorTheoryCommonWhat does the program print (lines separated by spaces)?
What does the program print (lines separated by spaces)?
чек №1 ок
Open full question →Common mistakes
- ✗Expecting
сообщитьorСООБЩИТЬto fail, as in case-sensitive languages: the built-in language ignores letter case in names - ✗Expecting the commented-out call to run: everything after
//to the end of the line is a comment
Follow-up questions
- →Why do the 1C standards still spell keywords one way if letter case does not matter?
- →Does letter case matter when two strings are compared with
=?
JuniorTheoryCommonWhat happens when the program runs?
What happens when the program runs?
Nothing prints: a compile error, variable Скидка is not defined
Open full question →Common mistakes
- ✗Expecting a variable assigned in a procedure to be visible in the module body: it is local, and the module needs
Перемto share it - ✗Expecting the lines before the error to run: this is a compile error, so the module never starts
Follow-up questions
- →How would you make
Скидкаvisible in the module body? - →Why does
Итогоchange insideДобавитьwhileСкидкаstays local?
JuniorTheoryCommonWhat does the program print (lines separated by spaces)?
What does the program print (lines separated by spaces)?
Чек КанцТовары Итого
Open full question →Common mistakes
- ✗Expecting the procedure at the top of the module to run first: it is only declared and runs when the module body calls it
- ✗Expecting procedures to run after the module body: a call jumps into the procedure at once and returns to the next line
Follow-up questions
- →What happens if a procedure is written after the module body?
- →How does the output change if
Шапка();is called twice?
JuniorTheoryCommonWhat does the program print?
What does the program print?
5
Open full question →Common mistakes
- ✗Expecting
0, as ifИначеbelonged to the outerЕсли: it stands inside and belongs to the nearestЕсли ЕстьКарта - ✗Expecting
Если ЕстьКартаto check that the variable is filled: the condition takes its value, hereЛожь
Follow-up questions
- →How would you write the same logic with
Иinstead of nesting? - →What happens if the inner
КонецЕсли;is missing?
JuniorTheoryCommonWhat does the program print (lines separated by spaces)?
What does the program print (lines separated by spaces)?
53 8
Open full question →Common mistakes
- ✗Expecting
8twice, as if+always adds when a number is involved: with a string on the left+joins strings - ✗Expecting a string on either side to make
+join: with a number on the left the string is converted to a number and added
Follow-up questions
- →What happens to
3 + "5шт"when the string is not a number? - →How do you add a number to a string explicitly with
Число()orСтрока()?
JuniorTheoryCommonWhat does the program print (lines separated by spaces)?
What does the program print (lines separated by spaces)?
4 3 -3
Open full question →Common mistakes
- ✗Expecting
Целto round 3.8 up to 4:Целdoes not round, it drops the fraction - ✗Expecting
Цел(-3.5)to give-4, as rounding down would:Целdrops the fraction toward zero and gives-3
Follow-up questions
- →Which function rounds
-3.5to-4, and why? - →How do you count full boxes and leftover pencils with
Целand%?
JuniorTheoryCommonHow does the conditional operator ?() work in the 1C built-in language?
How does the conditional operator ?() work in the 1C built-in language?
?(Условие, Значение1, Значение2) returns the first value when the condition is true and the second when it is false. Only the chosen branch is evaluated, so ?(Делитель = 0, 0, Сумма / Делитель) never divides by zero.
Common mistakes
- ✗Expecting both branches to be evaluated: only the branch the condition picks runs
- ✗Swapping the values: the first one is returned when the condition is true
Follow-up questions
- →When is
Если … Иначеbetter than?()even for a single assignment? - →What happens to readability when
?()calls are nested inside each other?
JuniorTheoryCommonWhat does the program print (lines separated by spaces)?
What does the program print (lines separated by spaces)?
Нет 151
Open full question →Common mistakes
- ✗Expecting
"15" + 1to give the number16: with a string on the left+joins strings and gives"151" - ✗Expecting a variable to have its own type that
+changes: the type belongs to the value, andХholds a string again
Follow-up questions
- →How do you turn the string
"15"into a number before adding? - →Why do the 1C standards check a type with
ТипЗнч(Х) = Тип("Число")?
JuniorTheoryCommonWhat does the program print (lines separated by spaces)?
What does the program print (lines separated by spaces)?
Да Нет Да
Open full question →Common mistakes
- ✗Treating
NullandНеопределеноas one value, like null in other languages: they differ, soСкидка = NullprintsНет - ✗Expecting
Сообщить(Истина)to print the wordИстина: a boolean prints asДаorНет
Follow-up questions
- →Where does a
Nullvalue come from in 1C code? - →What does
Сообщить(Скидка)print for a variable that holdsНеопределено?
JuniorTheoryCommonWhat does the program print (lines separated by spaces)?
What does the program print (lines separated by spaces)?
3 1
Open full question →Common mistakes
- ✗Expecting an extra pass, as if
Покаchecked its condition after the body: the check comes before every pass, so at 1 the body does not run - ✗Expecting
Покаto run the body once, likeЕсли: the body repeats while the condition is true
Follow-up questions
- →What happens if the loop body never changes
Остаток? - →What does the program print if
Остатокstarts at 2?
MiddleTheoryCommonWhere do you rely on short-circuit evaluation of И and ИЛИ as a guard?
Where do you rely on short-circuit evaluation of И and ИЛИ as a guard?
И stops at the first false operand and ИЛИ at the first true one, so the check goes first: Делитель <> 0 И Сумма / Делитель > 1, Список <> Неопределено И Список.Количество() > 0. Swapped, the risky part runs first.
Common mistakes
- ✗Putting the risky operand first: the guard must come before the division or the method call it protects
- ✗Writing a nested
Еслиfor every guard, not trustingИto skip the second operand - ✗Expecting
ИЛИto evaluate both sides: it stops at the first true operand
Follow-up questions
- →Does the conditional operator
?()also skip the branch it does not choose? - →How would you guard a property read on a value that may be
Неопределено?
MiddleTheoryOccasionalWhat does a module's main program section run, and when does it run?
What does a module's main program section run, and when does it run?
It is the statements after all procedures and functions, run once when the module instance is created: an object module's when the object is created, the application module's at startup. It usually sets module variables; a common module has none.
Common mistakes
- ✗Expecting the main program section to run before each method call: it runs once, when the module instance is created
- ✗Writing statements into the body of a common module: a common module holds only procedures and functions
- ✗Placing the section above the procedures: it goes after all of them
Follow-up questions
- →In what order must module variables, methods and the main program section appear?
- →Why is heavy code in an object module's main program section a performance risk?